Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/Documentation/howto/revert-a-faulty-merge.adoc # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / Documentation / howto / revert-a-faulty-merge.adoc` 274 lines ยท 10882 bytes Raw Blame History 1 Date: Fri, 19 Dec 2008 00:45:19 -0800 2 From: Linus Torvalds , Junio C Hamano 3 Subject: Re: Odd merge behaviour involving reverts 4 Abstract: Sometimes a branch that was already merged to the mainline 5 is later found to be faulty. Linus and Junio give guidance on 6 recovering from such a premature merge and continuing development 7 after the offending branch is fixed. 8 Message-ID: <7vocz8a6zk.fsf@gitster.siamese.dyndns.org> 9 References: 10 Content-type: text/asciidoc 11 12 How to revert a faulty merge 13 ============================ 14 15 Alan said: 16 17 I have a master branch. We have a branch off of that that some 18 developers are doing work on. They claim it is ready. We merge it 19 into the master branch. It breaks something so we revert the merge. 20 They make changes to the code. they get it to a point where they say 21 it is ok and we merge again. 22 23 When examined, we find that code changes made before the revert are 24 not in the master branch, but code changes after are in the master 25 branch. 26 27 and asked for help recovering from this situation. 28 29 The history immediately after the "revert of the merge" would look like 30 this: 31 32 ---o---o---o---M---x---x---W 33 / 34 ---A---B 35 36 where A and B are on the side development that was not so good, M is the 37 merge that brings these premature changes into the mainline, x are changes 38 unrelated to what the side branch did and already made on the mainline, 39 and W is the "revert of the merge M" (doesn't W look M upside down?). 40 IOW, `"diff W^..W"` is similar to `"diff -R M^..M"`. 41 42 Such a "revert" of a merge can be made with: 43 44 $ git revert -m 1 M 45 46 After the developers of the side branch fix their mistakes, the history 47 may look like this: 48 49 ---o---o---o---M---x---x---W---x 50 / 51 ---A---B-------------------C---D 52 53 where C and D are to fix what was broken in A and B, and you may already 54 have some other changes on the mainline after W. 55 56 If you merge the updated side branch (with D at its tip), none of the 57 changes made in A or B will be in the result, because they were reverted 58 by W. That is what Alan saw. 59 60 Linus explains the situation: 61 62 Reverting a regular commit just effectively undoes what that commit 63 did, and is fairly straightforward. But reverting a merge commit also 64 undoes the _data_ that the commit changed, but it does absolutely 65 nothing to the effects on _history_ that the merge had. 66 67 So the merge will still exist, and it will still be seen as joining 68 the two branches together, and future merges will see that merge as 69 the last shared state - and the revert that reverted the merge brought 70 in will not affect that at all. 71 72 So a "revert" undoes the data changes, but it's very much _not_ an 73 "undo" in the sense that it doesn't undo the effects of a commit on 74 the repository history. 75 76 So if you think of "revert" as "undo", then you're going to always 77 miss this part of reverts. Yes, it undoes the data, but no, it doesn't 78 undo history. 79 80 In such a situation, you would want to first revert the previous revert, 81 which would make the history look like this: 82 83 ---o---o---o---M---x---x---W---x---Y 84 / 85 ---A---B-------------------C---D 86 87 where Y is the revert of W. Such a "revert of the revert" can be done 88 with: 89 90 $ git revert W 91 92 This history would (ignoring possible conflicts between what W and W..Y 93 changed) be equivalent to not having W or Y at all in the history: 94 95 ---o---o---o---M---x---x-------x---- 96 / 97 ---A---B-------------------C---D 98 99 and merging the side branch again will not have conflict arising from an 100 earlier revert and revert of the revert. 101 102 ---o---o---o---M---x---x-------x-------* 103 / / 104 ---A---B-------------------C---D 105 106 Of course the changes made in C and D still can conflict with what was 107 done by any of the x, but that is just a normal merge conflict. 108 109 On the other hand, if the developers of the side branch discarded their 110 faulty A and B, and redone the changes on top of the updated mainline 111 after the revert, the history would have looked like this: 112 113 ---o---o---o---M---x---x---W---x---x 114 / \ 115 ---A---B A'--B'--C' 116 117 If you reverted the revert in such a case as in the previous example: 118 119 ---o---o---o---M---x---x---W---x---x---Y---* 120 / \ / 121 ---A---B A'--B'--C' 122 123 where Y is the revert of W, A' and B' are rerolled A and B, and there may 124 also be a further fix-up C' on the side branch. `"diff Y^..Y"` is similar 125 to `"diff -R W^..W"` (which in turn means it is similar to `"diff M^..M"`), 126 and `"diff A'^..C'"` by definition would be similar but different from that, 127 because it is a rerolled series of the earlier change. There will be a 128 lot of overlapping changes that result in conflicts. So do not do "revert 129 of revert" blindly without thinking.. 130 131 ---o---o---o---M---x---x---W---x---x 132 / \ 133 ---A---B A'--B'--C' 134 135 In the history with rebased side branch, W (and M) are behind the merge 136 base of the updated branch and the tip of the mainline, and they should 137 merge without the past faulty merge and its revert getting in the way. 138 139 To recap, these are two very different scenarios, and they want two very 140 different resolution strategies: 141 142 - If the faulty side branch was fixed by adding corrections on top, then 143 doing a revert of the previous revert would be the right thing to do. 144 145 - If the faulty side branch whose effects were discarded by an earlier 146 revert of a merge was rebuilt from scratch (i.e. rebasing and fixing, 147 as you seem to have interpreted), then re-merging the result without 148 doing anything else fancy would be the right thing to do. 149 (See the <> below for how to rebuild a branch 150 from scratch without changing its original branching-off point.) 151 152 However, there are things to keep in mind when reverting a merge (and 153 reverting such a revert). 154 155 For example, think about what reverting a merge (and then reverting the 156 revert) does to bisectability. Ignore the fact that the revert of a revert 157 is undoing it - just think of it as a "single commit that does a lot". 158 Because that is what it does. 159 160 When you have a problem you are chasing down, and you hit a "revert this 161 merge", what you're hitting is essentially a single commit that contains 162 all the changes (but obviously in reverse) of all the commits that got 163 merged. So it's debugging hell, because now you don't have lots of small 164 changes that you can try to pinpoint which _part_ of it changes. 165 166 But does it all work? Sure it does. You can revert a merge, and from a 167 purely technical angle, Git did it very naturally and had no real 168 troubles. It just considered it a change from "state before merge" to 169 "state after merge", and that was it. Nothing complicated, nothing odd, 170 nothing really dangerous. Git will do it without even thinking about it. 171 172 So from a technical angle, there's nothing wrong with reverting a merge, 173 but from a workflow angle it's something that you generally should try to 174 avoid. 175 176 If at all possible, for example, if you find a problem that got merged 177 into the main tree, rather than revert the merge, try _really_ hard to 178 bisect the problem down into the branch you merged, and just fix it, or 179 try to revert the individual commit that caused it. 180 181 Yes, it's more complex, and no, it's not always going to work (sometimes 182 the answer is: "oops, I really shouldn't have merged it, because it wasn't 183 ready yet, and I really need to undo _all_ of the merge"). So then you 184 really should revert the merge, but when you want to re-do the merge, you 185 now need to do it by reverting the revert. 186 187 [[ADDENDUM]] 188 ADDENDUM 189 190 Sometimes you have to rewrite one of a topic branch's commits *and* you can't 191 change the topic's branching-off point. Consider the following situation: 192 193 P---o---o---M---x---x---W---x 194 \ / 195 A---B---C 196 197 where commit W reverted commit M because it turned out that commit B was wrong 198 and needs to be rewritten, but you need the rewritten topic to still branch 199 from commit P (perhaps P is a branching-off point for yet another branch, and 200 you want be able to merge the topic into both branches). 201 202 The natural thing to do in this case is to checkout the A-B-C branch and use 203 "rebase -i P" to change commit B. However this does not rewrite commit A, 204 because "rebase -i" by default fast-forwards over any initial commits selected 205 with the "pick" command. So you end up with this: 206 207 P---o---o---M---x---x---W---x 208 \ / 209 A---B---C <-- old branch 210 \ 211 B'---C' <-- naively rewritten branch 212 213 To merge A-B'-C' into the mainline branch you would still have to first revert 214 commit W in order to pick up the changes in A, but then it's likely that the 215 changes in B' will conflict with the original B changes re-introduced by the 216 reversion of W. 217 218 However, you can avoid these problems if you recreate the entire branch, 219 including commit A: 220 221 A'---B'---C' <-- completely rewritten branch 222 / 223 P---o---o---M---x---x---W---x 224 \ / 225 A---B---C 226 227 You can merge A'-B'-C' into the mainline branch without worrying about first 228 reverting W. Mainline's history would look like this: 229 230 A'---B'---C'------------------ 231 / \ 232 P---o---o---M---x---x---W---x---M2 233 \ / 234 A---B---C 235 236 But if you don't actually need to change commit A, then you need some way to 237 recreate it as a new commit with the same changes in it. The rebase command's 238 --no-ff option provides a way to do this: 239 240 $ git rebase [-i] --no-ff P 241 242 The --no-ff option creates a new branch A'-B'-C' with all-new commits (all the 243 SHA IDs will be different) even if in the interactive case you only actually 244 modify commit B. You can then merge this new branch directly into the mainline 245 branch and be sure you'll get all of the branch's changes. 246 247 You can also use --no-ff in cases where you just add extra commits to the topic 248 to fix it up. Let's revisit the situation discussed at the start of this howto: 249 250 P---o---o---M---x---x---W---x 251 \ / 252 A---B---C----------------D---E <-- fixed-up topic branch 253 254 At this point, you can use --no-ff to recreate the topic branch: 255 256 $ git checkout E 257 $ git rebase --no-ff P 258 259 yielding 260 261 A'---B'---C'------------D'---E' <-- recreated topic branch 262 / 263 P---o---o---M---x---x---W---x 264 \ / 265 A---B---C----------------D---E 266 267 You can merge the recreated branch into the mainline without reverting commit W, 268 and mainline's history will look like this: 269 270 A'---B'---C'------------D'---E' 271 / \ 272 P---o---o---M---x---x---W---x---M2 273 \ / 274 A---B---C