Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which commit hash to undo a pushed merge using git-revert?

I merged the beta branch into the master branch. I pushed to origin. I now want master to be as it was prior to the merger both locally and remotely.

A good answer for undoing a merge that was already pushed suggests

git revert -m 1 commit_hash

If this is indeed the way to go, how can I determine commit_hash? I unsuccessfully tried the hash returned by merge-base:

$ git merge-base --all master beta
1f4b949b7ef97abf913ae672e3acd0907abfac1b
$ git revert -m 1 1f4b949b7ef97abf913ae672e3acd0907abfac1b
error: Mainline was specified but commit 1f4b949b7ef97abf913ae672e3acd0907abfac1b is not a merge.
fatal: revert failed

I've examined both git-log and gitk renditions of the branches, but they're very long, and I am uncertain enough of my interpretation to feel I should seek assistance before making a perhaps bigger mess. Beta was derived from v2 which was derived from master. There have been some mergers from master into v2 and beta along the way as I've kept the new branches up-to-date with master. The merger in the direction from beta into master was a mistake I wish to correct.

Once I do determine the merge point, if I find any commits made on master after the merger that really should be on the beta branch, what's the best way to move them over?

like image 673
kjhughes Avatar asked Jul 19 '12 14:07

kjhughes


People also ask

How do I revert a merge commit in Git?

git revert -m 1 <merge-commit>. With ‘-m 1’ we tell git to revert to the first parent of the mergecommit on the master branch. -m 2 would specify to revert to the first parent on the develop branch where the merge came from initially. Now commit the revert and push changes to the remote repo and you are done.

How to undo pushed-merge in Git?

With that extension all you need run is: git undo pushed-merge <merge-commit-hash>. then run a git log and get the id of the merge commit. then revert to that commit:

How to see parent commit hashes of merge commits in Git?

Here, we will assist you in doing that. Let's suppose that you are on the branch, on which the commit of the merge is. It is known that, in Git, the merge commits have two parent commits, and after running the git log command, in its output, you can see the parent commit hashes of the merge commit.

What is Git revert and how to use it?

The git revert operation takes the particular commit, inverses its changes, and generates a new “revert commit”. But you should also consider that git revert can undo a single commit and will not return to the previous state of your project.


2 Answers

You need to find the commit of the merge, git merge-base tells you the commit where you can do the merge. It basically is the last commit that exists in those two branches. The merge commit exists in your master branch only, unless you created a new branch after the merge, but that's not relevant here. :)

To find the merge commit try: git log master ^beta --ancestry-path --merges

The needed commit is the very last commit.

But please read up on Linus' write up: http://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.txt

like image 160
Peter van der Does Avatar answered Nov 08 '22 13:11

Peter van der Does


Also look at http://sethrobertson.github.com/GitFixUm/ which walks you through almost any git problem, including pushed merges. However...pushed merges have no easy solution.

like image 21
Seth Robertson Avatar answered Nov 08 '22 13:11

Seth Robertson