git revert <hash> not allowed due to a merge but no -m option was given
Asked Answered
A

2

62

I am trying to revert to a certain 'hash' number in git, by using the 'revert' command.

I am using the following command:

git revert c14609d74eec3ccebafc73fa875ec58445471765

But, I am getting the following returned:

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

What is happening & what should be done to resolve this?


I want to revert back to this certain commit (c14609d74eec3ccebafc73fa875ec58445471765) that I see when running git log.

Aprilette answered 19/6, 2014 at 7:48 Comment(1)
possible duplicate of Undo a Git merge?Meetinghouse
M
74

You are trying to revert a merge commit, and git doesn't know which parent to revert to. The -m allows us to choose which parent to choose. See the merge commit and note down which parent you want to go to. The parent information can be seen in git log, for example:

commit d02ee0f2179def10277f30c71c5d6f59ded3c595

Merge: dd3a24c 2462a52

and run:

git revert <hash> -m 1

where 1 indicates parent number 1 (dd3a24c).

If you are trying to revert to that commit, do:

git reset --hard <hash>

Understand the difference between git revert and git reset from the docs and decide which one you want. git revert is the safer option, but doesn't really do what you want. It just reverts the changes of a (set of) commit. git reset makes you move to a particular commit in history, and will rewrite your history.

Myogenic answered 19/6, 2014 at 7:52 Comment(4)
manojlds, what was the full git log command that you ran to see the parent?Hem
How do you determine which parent is which? i.e. How do I know to enter the number 1 v.s. 2 or something else?Nevsa
I originally merged a dev branch into master. When reverting the commit I used 1 as the value of -m which did the trickGunfight
@MatthewJamesBriggs I found this helpful answer to "How do you determine which parent is which?" https://blog.experteer.engineering/what-are-parents-on-git-merge-commits.htmlSuperinduce
I
4

I want to revert back to ...

Then you don't want git revert, at least not like this. git revert is for reverting the specific changes made in that commit. What you're looking for is to revert or undo all the changes made after that commit.

git reset is the command to use here.

git reset --hard c14609d74eec3ccebafc73fa875ec58445471765 completely resets your branch, index and work tree to that specific commit.

Note that the usual precautions apply: if anyone else has fetched later commits already, removing them from the history like this complicates matters for them. If you instead want to create a new commit, which simply restores the state to that of commit c14609d74eec3ccebafc73fa875ec58445471765, you can use git rm and git checkout:

git rm -r .
git checkout c14609d74eec3ccebafc73fa875ec58445471765 .

(The rm is needed to make sure newly added files also get removed.) This lets you then create a new commit, on top of your local history, which undoes every change since c14609d74eec3ccebafc73fa875ec58445471765.

Ipomoea answered 19/6, 2014 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.