271

I have run into a bit of a problem here: I had a problem-specific branch 28s in Git, that I merged in the general develop branch. Turns out I had done it too fast, so I used git-revert to undo the merge. Now, however, the time has come to merge 28s into develop, but git-merge command sees the original merge, and happily announces that all is well and branches have been already merged. What do I do now? Create a 'Revert "Revert "28s -> develop"" ' commit? Doesn't seem to be a good way to do it, but I can't imagine any other at the moment.

What the tree structure looks like:

Git log output

Scott Weldon
  • 8,032
  • 6
  • 43
  • 60
Toms Mikoss
  • 8,091
  • 10
  • 26
  • 39

8 Answers8

189

You have to "revert the revert". Depending on you how did the original revert, it may not be as easy as it sounds. Look at the official document on this topic.

---o---o---o---M---x---x---W---x---Y
              /
      ---A---B-------------------C---D

to allow:

---o---o---o---M---x---x-------x-------*
              /                       /
      ---A---B-------------------C---D

But does it all work? Sure it does. You can revert a merge, and from a purely technical angle, git did it very naturally and had no real troubles.
It just considered it a change from "state before merge" to "state after merge", and that was it.
Nothing complicated, nothing odd, nothing really dangerous. Git will do it without even thinking about it.

So from a technical angle, there's nothing wrong with reverting a merge, but from a workflow angle it's something that you generally should try to avoid.

If at all possible, for example, if you find a problem that got merged into the main tree, rather than revert the merge, try really hard to:

  • bisect the problem down into the branch you merged, and just fix it,
  • or try to revert the individual commit that caused it.

Yes, it's more complex, and no, it's not always going to work (sometimes the answer is: "oops, I really shouldn't have merged it, because it wasn't ready yet, and I really need to undo all of the merge"). So then you really should revert the merge, but when you want to re-do the merge, you now need to do it by reverting the revert.

Willem van Ketwich
  • 4,279
  • 7
  • 43
  • 51
J-16 SDiZ
  • 24,740
  • 3
  • 61
  • 82
  • 15
    Good link (+1). I took the liberty to copy part of the document in your answer in order to allow readers to see immediately the relevant options in this case. If you disagree, feel free to revert. – VonC Jul 03 '09 at 11:00
  • 5
    We just ran into a case where we needed to do this and found that the fun doesn't quite stop here. It was a long running branch that was merged in, so we needed to continue to update it. My approach here: http://tech.patientslikeme.com/2010/09/29/dealing-with-git-merge-revisions/ – jdwyah Sep 29 '10 at 15:07
  • i followed @jdwyah's blog post and it was glorious (seriously, it was awesome that it just worked). – hellatan Aug 20 '13 at 21:52
  • 2
    @jdwyah that seems to be a broken link, but it sounds like an interesting read. Here is a archive.org mirror but it is missing the images: https://web.archive.org/web/20111229193713/http://tech.patientslikeme.com/2010/09/29/dealing-with-git-merge-revisions – Alex KeySmith Jun 17 '14 at 15:44
  • Another link that also suggests "reverting the revert": [link](http://metlos.wordpress.com/2012/01/13/git-merging-after-a-revert/). – funroll Jul 07 '14 at 21:27
  • 16
    Blog post has been resurrected, thanks: http://blog.jdwyah.com/2015/07/dealing-with-git-merge-revisions.html – jdwyah Jul 07 '15 at 19:36
  • 1
    I followed @jdwyah post too. It worked great, I would only change the 'git revert SHA_OF_THE_MERGE_REVERSION' for 'SHA_OF_THE_REVERT_MERGE' or something similar. – mrmuggles Oct 24 '15 at 15:19
  • "revert the revert" :D. Can't imagine that I'm doing that :D – Ahmed Mahmoud Jun 05 '18 at 07:13
64

Let's assume you have such history

---o---o---o---M---W---x-------x-------*
              /                      
      ---A---B

Where A, B failed commits and W - is revert of M

So before I start fixing found problems I do cherry-pick of W commit to my branch

git cherry-pick -x W

Then I revert W commit on my branch

git revert W 

After I can continue fixing.

The final history could look like:

---o---o---o---M---W---x-------x-------*
              /                       /     
      ---A---B---W---W`----------C---D

When I send a PR it will clearly shows that PR is undo revert and adds some new commits.

Maksim Kotlyar
  • 3,593
  • 25
  • 31
28

To revert the revert without screwing up your workflow too much:

  • Create a local trash copy of develop
  • Revert the revert commit on the local copy of develop
  • Merge that copy into your feature branch, and push your feature branch to your git server.

Your feature branch should now be able to be merged as normal when you're ready for it. The only downside here is that you'll a have a few extra merge/revert commits in your history.

Sam Dufel
  • 16,546
  • 3
  • 43
  • 49
  • Just to prevent any further mix ups I also created a 'trash' copy of my feature branch and merged the reverted develop into it. – jhhwilliams Feb 01 '19 at 14:35
  • Thank you! This is the only answer that actually explains how to do it instead of saying you shouldn't do this. Really helpfull. – Emmy Dec 18 '19 at 14:36
  • Thank you, this really helped me. :) – Daniel Apr 09 '20 at 14:26
  • this is a very straightforward answer. – levi Nov 19 '20 at 03:05
  • thank you, my case was pretty simple so this totally got the job done for me. The other answers are awesome too if you are in a more complex situation. – subelsky Feb 05 '21 at 20:47
16

To revert a revert in GIT:

git revert <commit-hash-of-previous-revert>
Richard
  • 1,551
  • 16
  • 19
  • 1
    Using this in my work branch to revert the revert, then new PR to develop. Now git sees all changes that were in the previous PR that was reverted. Thanks. – Danstan Mar 03 '20 at 06:44
5

Instead of using git-revert you could have used this command in the devel branch to throw away (undo) the wrong merge commit (instead of just reverting it).

git checkout devel
git reset --hard COMMIT_BEFORE_WRONG_MERGE

This will also adjust the contents of the working directory accordingly. Be careful:

  • Save your changes in the develop branch (since the wrong merge) because they too will be erased by the git-reset. All commits after the one you specify as the git reset argument will be gone!
  • Also, don't do this if your changes were already pulled from other repositories because the reset will rewrite history.

I recommend to study the git-reset man-page carefully before trying this.

Now, after the reset you can re-apply your changes in devel and then do

git checkout devel
git merge 28s

This will be a real merge from 28s into devel like the initial one (which is now erased from git's history).

knweiss
  • 445
  • 4
  • 5
  • 10
    For anyone who is not super familiar with git and might want to follow these instructions: careful with combining `reset --hard` and `push origin`. Also be aware a force push to origin may really muck up open PRs on GitHub. – funroll Jul 07 '14 at 21:21
  • Very helpful for fixing some merge issues on a private git server. Thanks! – mix3d Jun 25 '15 at 15:20
  • 1
    +1 for this technique. Potentially destructive, but can save you a lot of headache (and a mangled history) when applied judiciously. – siliconrockstar May 25 '17 at 20:34
2

I would suggest you to follow below steps to revert a revert, say SHA1.

git checkout develop #go to develop branch
git pull             #get the latest from remote/develop branch
git branch users/yourname/revertOfSHA1 #having HEAD referring to develop
git checkout users/yourname/revertOfSHA1 #checkout the newly created branch
git log --oneline --graph --decorate #find the SHA of the revert in the history, say SHA1
git revert SHA1
git push --set-upstream origin users/yourname/revertOfSHA1 #push the changes to remote

Now create PR for the branch users/yourname/revertOfSHA1

Venkataraman R
  • 8,468
  • 1
  • 21
  • 39
1

I just found this post when facing the same problem. I find above wayyy to scary to do reset hards etc. I'll end up deleting something I don't want to, and won't be able to get it back.

Instead I checked out the commit I wanted the branch to go back to e.g. git checkout 123466t7632723. Then converted to a branch git checkout my-new-branch. I then deleted the branch I didn't want any more. Of course this will only work if you are able to throw away the branch you messed up.

Claire
  • 3,383
  • 11
  • 41
  • 72
  • 1
    The `git reflog` will protect you on a hard reset for a couple of months in case you later discover that you need the lost commits. The reflog is limited to your local repo. – Todd Jan 05 '18 at 22:32
1
  1. create new branch at commit prior to the original merge - call it it 'develop-base'
  2. perform interactive rebase of 'develop' on top of 'develop-base' (even though it's already on top). During interactive rebase, you'll have the opportunity to remove both the merge commit, and the commit that reversed the merge, i.e. remove both events from git history

At this point you'll have a clean 'develop' branch to which you can merge your feature brach as you regularly do.