Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undoing a Fast-Forward Merge

I had a bad merge, I did not know it was a bad merge until I pushed it to GitHub and now I do not know how to undo/revert it.

The story goes like this:

I opened two distinct branches and committed several changes to those branches. After a while, I decided to merge those branches back to master branch and I use $ git merge branch_name for this purpose. I merged the first branch and checked out the log of master branch afterwards: all commits on that branch were on master. Then I proceeded with the merging of second branch by the same command. Then I checked the log of master again, where I saw the commits on the branch I merged including a commit with the following comment: "merge branch branch_name". A comment like this was not included when I merged the other branch.

Long story short, the problem is that git merged the commits of the first branch to master like they are the commits of the master already. When you examine the network graph from 'GitHub' or the graph from 'gitg', there is no trace of the first branch left, but its commits are shown as the commits of master branch. There is no problem with the second branch: it is shown separately and it is merged as expected. Also note that, after running the merge commands, I went on deleting the branches by $ git branch -d branch_name, like it was the most urgent thing to do, unfortunately.

Now I want to go back to the position just before I ran two consecutive merges. Any help is appreciated. If you comment on why this has happened, it will be pleasing as well.

like image 424
clancularius Avatar asked Oct 05 '22 23:10

clancularius


1 Answers

  1. Find the commits that were the heads of the old topic branches that you deleted. They should be the two parents of the merge commit. Make them into branches again (git branch or git checkout -b will do the trick nicely).

  2. Move master back to its old position with git reset. At this point, you're back where you started before you merged.

  3. Next time when you merge, use git merge --no-ff. This will create a merge commit even if Git doesn't need to.

I almost always use git merge with either --ff-only or --no-ff.

like image 143
Dietrich Epp Avatar answered Oct 13 '22 11:10

Dietrich Epp