Skip to content

Commit 365aa19

Browse files
J. Bruce FieldsJunio C Hamano
authored andcommitted
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>
1 parent 3512193 commit 365aa19

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Documentation/user-manual.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ with the changes to be reverted, then you will be asked to fix
13331333
conflicts manually, just as in the case of <<resolving-a-merge,
13341334
resolving a merge>>.
13351335

1336+
[[fixing-a-mistake-by-editing-history]]
13361337
Fixing a mistake by editing history
13371338
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13381339

@@ -1935,6 +1936,51 @@ return mywork to the state it had before you started the rebase:
19351936
$ git rebase --abort
19361937
-------------------------------------------------
19371938

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+
19381984
Reordering or selecting from a patch series
19391985
-------------------------------------------
19401986

0 commit comments

Comments
 (0)