1111How to revert an existing commit
1212================================
1313
14- One of the changes I pulled into the 'master ' branch turns out to
14+ One of the changes I pulled into the 'main ' branch turns out to
1515break building Git with GCC 2.95. While they were well-intentioned
1616portability fixes, keeping things working with gcc-2.95 was also
17- important. Here is what I did to revert the change in the 'master '
17+ important. Here is what I did to revert the change in the 'main '
1818branch and to adjust the 'seen' branch, using core Git tools and
1919barebone Porcelain.
2020
2121First, prepare a throw-away branch in case I screw things up.
2222
2323------------------------------------------------
24- $ git checkout -b revert-c99 master
24+ $ git checkout -b revert-c99 main
2525------------------------------------------------
2626
2727Now 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
28+ revert. I happen to know that the top of the 'main ' branch is a
2929merge, and its second parent (i.e. foreign commit I merged from) has
3030the change I would want to undo. Further I happen to know that that
3131merge introduced 5 commits or so:
3232
3333------------------------------------------------
34- $ git show-branch --more=4 master master ^2 | head
35- * [master ] Merge refs/heads/portable from http://www.cs.berkeley....
36- ! [master ^2] Replace C99 array initializers with code.
34+ $ git show-branch --more=4 main main ^2 | head
35+ * [main ] Merge refs/heads/portable from http://www.cs.berkeley....
36+ ! [main ^2] Replace C99 array initializers with code.
3737--
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
38+ - [main ] Merge refs/heads/portable from http://www.cs.berkeley....
39+ *+ [main ^2] Replace C99 array initializers with code.
40+ *+ [main ^2~1] Replace unsetenv() and setenv() with older putenv().
41+ *+ [main ^2~2] Include sys/time.h in daemon.c.
42+ *+ [main ^2~3] Fix ?: statements.
43+ *+ [main ^2~4] Replace zero-length array decls with [].
44+ * [main ~1] tutorial note about git branch
4545------------------------------------------------
4646
4747The '--more=4' above means "after we reach the merge base of refs,
@@ -51,15 +51,15 @@ git.git repository, so this would show everything on both branches
5151since then. I just limited the output to the first handful using
5252'head'.
5353
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
54+ Now I know 'main ^2~4' (pronounce it as "find the second parent of
55+ the 'main ', and then go four generations back following the first
5656parent") is the one I would want to revert. Since I also want to say
5757why I am reverting it, the '-n' flag is given to 'git revert'. This
5858prevents it from actually making a commit, and instead 'git revert'
5959leaves the commit log message it wanted to use in '.msg' file:
6060
6161------------------------------------------------
62- $ git revert -n master ^2~4
62+ $ git revert -n main ^2~4
6363$ cat .msg
6464Revert "Replace zero-length array decls with []."
6565
@@ -79,12 +79,12 @@ $ git commit -a -s ;# read .msg into the log,
7979------------------------------------------------
8080
8181I 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.
82+ case I could just have done 'git checkout main ' to start over.
8383Fortunately 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 ':
84+ 'revert-c99' is what I want. So merge that back into 'main ':
8585
8686------------------------------------------------
87- $ git checkout master
87+ $ git checkout main
8888$ git merge revert-c99 ;# this should be a fast-forward
8989Updating from 10d781b9caa4f71495c7b34963bef137216f86a8 to e3a693c...
9090 cache.h | 8 ++++----
@@ -96,10 +96,10 @@ Updating from 10d781b9caa4f71495c7b34963bef137216f86a8 to e3a693c...
9696------------------------------------------------
9797
9898There is no need to redo the test at this point. We fast-forwarded
99- and we know 'master ' matches 'revert-c99' exactly. In fact:
99+ and we know 'main ' matches 'revert-c99' exactly. In fact:
100100
101101------------------------------------------------
102- $ git diff master ..revert-c99
102+ $ git diff main ..revert-c99
103103------------------------------------------------
104104
105105says nothing.
@@ -109,7 +109,7 @@ Then we rebase the 'seen' branch as usual.
109109------------------------------------------------
110110$ git checkout seen
111111$ git tag seen-anchor seen
112- $ git rebase master
112+ $ git rebase main
113113* Applying: Redo "revert" using three-way merge machinery.
114114First trying simple merge strategy to cherry-pick.
115115* Applying: Remove git-apply-patch-script.
@@ -131,7 +131,7 @@ The temporary tag 'seen-anchor' is me just being careful, in case 'git
131131rebase' screws up. After this, I can do these for sanity check:
132132
133133------------------------------------------------
134- $ git diff seen-anchor..seen ;# make sure we got the master fix.
134+ $ git diff seen-anchor..seen ;# make sure we got the main fix.
135135$ make CC=gcc-2.95 clean test ;# make sure it fixed the breakage.
136136$ make clean test ;# make sure it did not cause other breakage.
137137------------------------------------------------
@@ -150,11 +150,11 @@ be some days off:
150150
151151------------------------------------------------
152152$ git checkout rc
153- $ git pull . master
153+ $ git pull . main
154154Packing 0 objects
155155Unpacking 0 objects
156156
157- * commit-ish: e3a693c... refs/heads/master from .
157+ * commit-ish: e3a693c... refs/heads/main from .
158158Trying to merge e3a693c... into 8c1f5f0... using 10d781b...
159159Committed merge 7fb9b7262a1d1e0a47bbfdcbbcf50ce0635d3f8f
160160 cache.h | 8 ++++----
@@ -168,10 +168,10 @@ Committed merge 7fb9b7262a1d1e0a47bbfdcbbcf50ce0635d3f8f
168168And the final repository status looks like this:
169169
170170------------------------------------------------
171- $ git show-branch --more=1 master seen rc
172- ! [master ] Revert "Replace zero-length array decls with []."
171+ $ git show-branch --more=1 main seen rc
172+ ! [main ] Revert "Replace zero-length array decls with []."
173173 ! [seen] git-repack: Add option to repack all objects.
174- * [rc] Merge refs/heads/master from .
174+ * [rc] Merge refs/heads/main from .
175175---
176176 + [seen] git-repack: Add option to repack all objects.
177177 + [seen~1] More documentation updates.
@@ -180,8 +180,8 @@ $ git show-branch --more=1 master seen rc
180180 + [seen~4] Document "git cherry-pick" and "git revert"
181181 + [seen~5] Remove git-apply-patch-script.
182182 + [seen~6] Redo "revert" using three-way merge machinery.
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....
183+ - [rc] Merge refs/heads/main from .
184+ ++* [main ] Revert "Replace zero-length array decls with []."
185+ - [rc~1] Merge refs/heads/main from .
186+ ... [main ~1] Merge refs/heads/portable from http://www.cs.berkeley....
187187------------------------------------------------
0 commit comments