]>
Commit | Line | Data |
---|---|---|
59eb68aa | 1 | From: Junio C Hamano <gitster@pobox.com> |
3459c2c4 JH |
2 | To: git@vger.kernel.org |
3 | Subject: [HOWTO] Reverting an existing commit | |
4 | Abstract: In this article, JC gives a small real-life example of using | |
5 | 'git revert' command, and using a temporary branch and tag for safety | |
6 | and easier sanity checking. | |
7 | Date: Mon, 29 Aug 2005 21:39:02 -0700 | |
8 | Content-type: text/asciidoc | |
9 | Message-ID: <7voe7g3uop.fsf@assigned-by-dhcp.cox.net> | |
10 | ||
1797e5c5 TA |
11 | How to revert an existing commit |
12 | ================================ | |
3459c2c4 JH |
13 | |
14 | One of the changes I pulled into the 'master' branch turns out to | |
17b83d71 | 15 | break building Git with GCC 2.95. While they were well-intentioned |
3459c2c4 JH |
16 | portability fixes, keeping things working with gcc-2.95 was also |
17 | important. Here is what I did to revert the change in the 'master' | |
77dc6049 | 18 | branch and to adjust the 'seen' branch, using core Git tools and |
3459c2c4 JH |
19 | barebone Porcelain. |
20 | ||
21 | First, prepare a throw-away branch in case I screw things up. | |
22 | ||
23 | ------------------------------------------------ | |
24 | $ git checkout -b revert-c99 master | |
25 | ------------------------------------------------ | |
26 | ||
27 | Now I am on the 'revert-c99' branch. Let's figure out which commit to | |
28 | revert. I happen to know that the top of the 'master' branch is a | |
29 | merge, and its second parent (i.e. foreign commit I merged from) has | |
30 | the change I would want to undo. Further I happen to know that that | |
31 | merge introduced 5 commits or so: | |
32 | ||
33 | ------------------------------------------------ | |
34 | $ git show-branch --more=4 master master^2 | head | |
ebedc319 | 35 | * [master] Merge refs/heads/portable from http://www.cs.berkeley.... |
3459c2c4 JH |
36 | ! [master^2] Replace C99 array initializers with code. |
37 | -- | |
ebedc319 JH |
38 | - [master] Merge refs/heads/portable from http://www.cs.berkeley.... |
39 | *+ [master^2] Replace C99 array initializers with code. | |
40 | *+ [master^2~1] Replace unsetenv() and setenv() with older putenv(). | |
41 | *+ [master^2~2] Include sys/time.h in daemon.c. | |
42 | *+ [master^2~3] Fix ?: statements. | |
43 | *+ [master^2~4] Replace zero-length array decls with []. | |
44 | * [master~1] tutorial note about git branch | |
3459c2c4 JH |
45 | ------------------------------------------------ |
46 | ||
47 | The '--more=4' above means "after we reach the merge base of refs, | |
48 | show until we display four more common commits". That last commit | |
49 | would have been where the "portable" branch was forked from the main | |
50 | git.git repository, so this would show everything on both branches | |
51 | since then. I just limited the output to the first handful using | |
52 | 'head'. | |
53 | ||
54 | Now I know 'master^2~4' (pronounce it as "find the second parent of | |
55 | the 'master', and then go four generations back following the first | |
56 | parent") is the one I would want to revert. Since I also want to say | |
57 | why I am reverting it, the '-n' flag is given to 'git revert'. This | |
58 | prevents it from actually making a commit, and instead 'git revert' | |
59 | leaves the commit log message it wanted to use in '.msg' file: | |
60 | ||
61 | ------------------------------------------------ | |
62 | $ git revert -n master^2~4 | |
63 | $ cat .msg | |
64 | Revert "Replace zero-length array decls with []." | |
65 | ||
66 | This reverts 6c5f9baa3bc0d63e141e0afc23110205379905a4 commit. | |
67 | $ git diff HEAD ;# to make sure what we are reverting makes sense. | |
68 | $ make CC=gcc-2.95 clean test ;# make sure it fixed the breakage. | |
69 | $ make clean test ;# make sure it did not cause other breakage. | |
70 | ------------------------------------------------ | |
71 | ||
72 | The reverted change makes sense (from reading the 'diff' output), does | |
73 | fix the problem (from 'make CC=gcc-2.95' test), and does not cause new | |
74 | breakage (from the last 'make test'). I'm ready to commit: | |
75 | ||
76 | ------------------------------------------------ | |
77 | $ git commit -a -s ;# read .msg into the log, | |
78 | # and explain why I am reverting. | |
79 | ------------------------------------------------ | |
80 | ||
81 | I could have screwed up in any of the above steps, but in the worst | |
82 | case I could just have done 'git checkout master' to start over. | |
83 | Fortunately I did not have to; what I have in the current branch | |
84 | 'revert-c99' is what I want. So merge that back into 'master': | |
85 | ||
86 | ------------------------------------------------ | |
87 | $ git checkout master | |
a75d7b54 | 88 | $ git merge revert-c99 ;# this should be a fast-forward |
3459c2c4 JH |
89 | Updating from 10d781b9caa4f71495c7b34963bef137216f86a8 to e3a693c... |
90 | cache.h | 8 ++++---- | |
91 | commit.c | 2 +- | |
92 | ls-files.c | 2 +- | |
93 | receive-pack.c | 2 +- | |
94 | server-info.c | 2 +- | |
95 | 5 files changed, 8 insertions(+), 8 deletions(-) | |
96 | ------------------------------------------------ | |
97 | ||
a75d7b54 | 98 | There is no need to redo the test at this point. We fast-forwarded |
3459c2c4 JH |
99 | and we know 'master' matches 'revert-c99' exactly. In fact: |
100 | ||
101 | ------------------------------------------------ | |
102 | $ git diff master..revert-c99 | |
103 | ------------------------------------------------ | |
104 | ||
105 | says nothing. | |
106 | ||
77dc6049 | 107 | Then we rebase the 'seen' branch as usual. |
3459c2c4 JH |
108 | |
109 | ------------------------------------------------ | |
77dc6049 JS |
110 | $ git checkout seen |
111 | $ git tag seen-anchor seen | |
3459c2c4 JH |
112 | $ git rebase master |
113 | * Applying: Redo "revert" using three-way merge machinery. | |
114 | First trying simple merge strategy to cherry-pick. | |
3459c2c4 JH |
115 | * Applying: Remove git-apply-patch-script. |
116 | First trying simple merge strategy to cherry-pick. | |
117 | Simple cherry-pick fails; trying Automatic cherry-pick. | |
118 | Removing Documentation/git-apply-patch-script.txt | |
119 | Removing git-apply-patch-script | |
3459c2c4 JH |
120 | * Applying: Document "git cherry-pick" and "git revert" |
121 | First trying simple merge strategy to cherry-pick. | |
3459c2c4 JH |
122 | * Applying: mailinfo and applymbox updates |
123 | First trying simple merge strategy to cherry-pick. | |
3459c2c4 JH |
124 | * Applying: Show commits in topo order and name all commits. |
125 | First trying simple merge strategy to cherry-pick. | |
3459c2c4 JH |
126 | * Applying: More documentation updates. |
127 | First trying simple merge strategy to cherry-pick. | |
3459c2c4 JH |
128 | ------------------------------------------------ |
129 | ||
77dc6049 | 130 | The temporary tag 'seen-anchor' is me just being careful, in case 'git |
3459c2c4 JH |
131 | rebase' screws up. After this, I can do these for sanity check: |
132 | ||
133 | ------------------------------------------------ | |
77dc6049 | 134 | $ git diff seen-anchor..seen ;# make sure we got the master fix. |
3459c2c4 JH |
135 | $ make CC=gcc-2.95 clean test ;# make sure it fixed the breakage. |
136 | $ make clean test ;# make sure it did not cause other breakage. | |
137 | ------------------------------------------------ | |
138 | ||
139 | Everything is in the good order. I do not need the temporary branch | |
a58088ab | 140 | or tag anymore, so remove them: |
3459c2c4 JH |
141 | |
142 | ------------------------------------------------ | |
77dc6049 | 143 | $ rm -f .git/refs/tags/seen-anchor |
e66ab03f | 144 | $ git branch -d revert-c99 |
3459c2c4 JH |
145 | ------------------------------------------------ |
146 | ||
147 | It was an emergency fix, so we might as well merge it into the | |
148 | 'release candidate' branch, although I expect the next release would | |
149 | be some days off: | |
150 | ||
151 | ------------------------------------------------ | |
152 | $ git checkout rc | |
153 | $ git pull . master | |
154 | Packing 0 objects | |
155 | Unpacking 0 objects | |
156 | ||
a8a5406a | 157 | * commit-ish: e3a693c... refs/heads/master from . |
3459c2c4 JH |
158 | Trying to merge e3a693c... into 8c1f5f0... using 10d781b... |
159 | Committed merge 7fb9b7262a1d1e0a47bbfdcbbcf50ce0635d3f8f | |
160 | cache.h | 8 ++++---- | |
161 | commit.c | 2 +- | |
162 | ls-files.c | 2 +- | |
163 | receive-pack.c | 2 +- | |
164 | server-info.c | 2 +- | |
165 | 5 files changed, 8 insertions(+), 8 deletions(-) | |
166 | ------------------------------------------------ | |
167 | ||
168 | And the final repository status looks like this: | |
169 | ||
170 | ------------------------------------------------ | |
77dc6049 | 171 | $ git show-branch --more=1 master seen rc |
3459c2c4 | 172 | ! [master] Revert "Replace zero-length array decls with []." |
77dc6049 | 173 | ! [seen] git-repack: Add option to repack all objects. |
3459c2c4 JH |
174 | * [rc] Merge refs/heads/master from . |
175 | --- | |
77dc6049 JS |
176 | + [seen] git-repack: Add option to repack all objects. |
177 | + [seen~1] More documentation updates. | |
178 | + [seen~2] Show commits in topo order and name all commits. | |
179 | + [seen~3] mailinfo and applymbox updates | |
180 | + [seen~4] Document "git cherry-pick" and "git revert" | |
181 | + [seen~5] Remove git-apply-patch-script. | |
182 | + [seen~6] Redo "revert" using three-way merge machinery. | |
ebedc319 JH |
183 | - [rc] Merge refs/heads/master from . |
184 | ++* [master] Revert "Replace zero-length array decls with []." | |
185 | - [rc~1] Merge refs/heads/master from . | |
186 | ... [master~1] Merge refs/heads/portable from http://www.cs.berkeley.... | |
3459c2c4 | 187 | ------------------------------------------------ |