@matheusbsbs / matheus-bagliano / commits / 365aa19919
Fork 0

user-manual: how to replace commits older than most recent

"Modifying" an old commit by checking it out, --amend'ing it, then rebasing on top of it, is a slightly cumbersome technique, but I've found it useful frequently enough to make it seem worth documenting. Signed-off-by: "J. Bruce Fields" <bfields@citi.umich.edu> Signed-off-by: Junio C Hamano <junkio@cox.net>

J. Bruce Fields committed Mar 3, 2007 at 14:30 UTC 365aa199196c8665e978ec7092323167621a69ea
1 file changed +46
Documentation/user-manual.txt
+46
@@ -1333,6 +1333,7 @@ with the changes to be reverted, then you will be asked to fix
1333 conflicts manually, just as in the case of <<resolving-a-merge,
1334 resolving a merge>>.
1335
1336 +[[fixing-a-mistake-by-editing-history]]
1337 Fixing a mistake by editing history
1338 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1339
@@ -1935,6 +1936,51 @@ return mywork to the state it had before you started the rebase:
1936 $ git rebase --abort
1937 -------------------------------------------------
1938
1939 +Modifying a single commit
1940 +-------------------------
1941 +
1942 +We saw in <<fixing-a-mistake-by-editing-history>> that you can replace the
1943 +most recent commit using
1944 +
1945 +-------------------------------------------------
1946 +$ git commit --amend
1947 +-------------------------------------------------
1948 +
1949 +which will replace the old commit by a new commit incorporating your
1950 +changes, giving you a chance to edit the old commit message first.
1951 +
1952 +You can also use a combination of this and gitlink:git-rebase[1] to edit
1953 +commits further back in your history. First, tag the problematic commit with
1954 +
1955 +-------------------------------------------------
1956 +$ git tag bad mywork~5
1957 +-------------------------------------------------
1958 +
1959 +(Either gitk or git-log may be useful for finding the commit.)
1960 +
1961 +Then check out a new branch at that commit, edit it, and rebase the rest of
1962 +the series on top of it:
1963 +
1964 +-------------------------------------------------
1965 +$ git checkout -b TMP bad
1966 +$ # make changes here and update the index
1967 +$ git commit --amend
1968 +$ git rebase --onto TMP bad mywork
1969 +-------------------------------------------------
1970 +
1971 +When you're done, you'll be left with mywork checked out, with the top patches
1972 +on mywork reapplied on top of the modified commit you created in TMP. You can
1973 +then clean up with
1974 +
1975 +-------------------------------------------------
1976 +$ git branch -d TMP
1977 +$ git tag -d bad
1978 +-------------------------------------------------
1979 +
1980 +Note that the immutable nature of git history means that you haven't really
1981 +"modified" existing commits; instead, you have replaced the old commits with
1982 +new commits having new object names.
1983 +
1984 Reordering or selecting from a patch series
1985 -------------------------------------------
1986