3

I recently merged a local feature branch to my local master branch using git. Then I made a few changes and commits to the master branch. Now, I want to get rid of the changes caused by the merge on my master branch. So I did a:

git revert <commit-id>

However, I got error

error: Commit c49aaca4acf461cc41390c1d1d3477f09e25a368 is a merge but no -m option was given. fatal: revert failed

FYI, I haven't pushed anything to the server. I searched online but I couldn't find a decent solution to that. Does anyone know how to do it? Thanks.

iefgnoix
  • 71
  • 2
  • 10
  • See http://stackoverflow.com/q/2389361/1256452, which is not exactly a duplicate, but does contain many correct answers. The main issues here are whether you have *pushed* this merge, and how much work you want to do *now* to keep post-merge changes, vs how much work you want to defer to a later time, to *restore* the merged-in changes. – torek Mar 13 '17 at 18:30
  • I haven't yet pushed the merge. I read the link that you provided. But in my case, I have already made a few changes and commits to my master branch. I am just wondering if there is a way to only get rid of that one merge while keep all the post-merge changes as is. – iefgnoix Mar 13 '17 at 20:01
  • OK, if you haven't *pushed* yet, I don't have time for a proper answer but you can use `git rebase` to *copy* all the non-merge commits. Rebase normally strips out merges entirely, so, with no one else having the original commits you now dislike because they have a merge in them, your copies-minus-the-merge should be exactly what you want. – torek Mar 13 '17 at 20:04

1 Answers1

6

In Git, a commit usually references only 1 parent commit.

However, if you merge and got conflicts, if you merge with the option --no-ff (no fast forward) or if Git can't fast-forward, Git create a merge commit.

A merge commit is a commit dedicated to link two branches by referencing the 2 commits that are the tip of the branches (2 parents in this case).

When you want to revert this type of commit, you need to specify which branch should be the remaining one. As stated in Git doc :

Reverting a merge commit declares that you will never want the tree changes brought in by the merge

You have to revert with the option -m (--mainline) with a parent number. The number of the branch you merged in is 1 and the number of the merged branch is 2.

In your case, to go back to the state of your master branch before the merge of your feature branch, you should revert with the command

git revert c49aaca4acf461cc41390c1d1d3477f09e25a368 -m 1

Tip: before launching the revert, create a new branch from your master (assuming you are currently on it)

git checkout -b you-name-it

and execute the revert on this new branch : if it has the expected result, checkout again your master branch and perform the revert, if it hasn't you still have your master branch intact.

Community
  • 1
  • 1
Lex Lustor
  • 1,525
  • 2
  • 22
  • 27
  • I neither got conflicts when I did git merge nor merged with the option --no-ff. – iefgnoix Mar 13 '17 at 19:57
  • @iefgnoix So when you `merged`, Git couldn't fast forward and created a merge commit, I will update my answer with this case. Did you try to revert specifying the mainline ? – Lex Lustor Mar 13 '17 at 20:09