git revert
alone won\'t work. -m
must be specified, and I\'m pretty confused about it.
Anyone experienced this before?<
All the answers already covered most of the things but I will add my 5 cents. In short reverting a merge commit is quite simple:
git revert -m 1 <commit-hash>
If you have permission you can push it directly to the "master" branch otherwise simply push it to your "revert" branch and create pull request.
You might find more useful info on this subject here: https://itcodehub.blogspot.com/2019/06/how-to-revert-merge-in-git.html
I also faced this issue on a PR that has been merged to the master branch of a GitHub repo.
Since I just wanted to modify some modified files but not the whole changes the PR brought, I had to amend
the merge commit
with git commit --am
.
Steps:
git add *
or git add <file>
git commit --am
and validategit push -f
Why it's interesting:
Here's a complete example in the hope that it helps someone:
git revert -m 1 <commit-hash>
git push -u origin master
Where <commit-hash>
is the commit hash of the merge that you would like to revert, and as stated in the explanation of this answer, -m 1
indicates that you'd like to revert to the tree of the first parent prior to the merge.
The git revert ...
line essentially commits your changes while the second line makes your changes public by pushing them to the remote branch.
The -m
option specifies the parent number. This is because a merge commit has more than one parent, and Git does not know automatically which parent was the mainline, and which parent was the branch you want to un-merge.
When you view a merge commit in the output of git log
, you will see its parents listed on the line that begins with Merge
:
commit 8f937c683929b08379097828c8a04350b9b8e183
Merge: 8989ee0 7c6b236
Author: Ben James <ben@example.com>
Date: Wed Aug 17 22:49:41 2011 +0100
Merge branch 'gh-pages'
Conflicts:
README
In this situation, git revert 8f937c6 -m 1
will get you the tree as it was in 8989ee0
, and git revert -m 2
will reinstate the tree as it was in 7c6b236
.
To better understand the parent IDs, you can run:
git log 8989ee0
and
git log 7c6b236
I found creating a reverse patch between two know end-points and applying that patch would work. This presumes that you have created snapshots (tags) off of your master branch or even a back up of your master branch say master_bk_01012017.
Say the code branch you merged into master was mycodebranch.
git diff --binary master..master_bk_01012017 > ~/myrevert.patch
git apply --check myrevert.patch
git am --signoff < myrevert.patch
git branch mycodebranch_fix
git checkout mycodebranch_fix
git revert [SHA]
The correctly marked answer worked for me but I had to spend some time to determine whats going on.. So I decided to add an answer with simple straightforward steps for cases like mine..
Lets say we got branches A and B.. You merged branch A into branch B and pushed branch B to itself so now the merge is part of it.. But you want to go back to the last commit before the merge.. What do you do?
git log
You will see the history of recent commits - the commits have commit/author/date properties while the merges also have a merge property - so you see them like this:
commit: <commitHash>
Merge: <parentHashA> <parentHashB>
Author: <author>
Date: <date>
Use git log <parentHashA>
and git log <parentHashB>
- you will see the commit histories of those parent branches - the first commits in the list are the latest ones
<commitHash>
of the commit you want, go to your git root folder and use git checkout -b <newBranchName> <commitHash>
- that will create a new branch starting from that last commit you've chosen before the merge.. Voila, ready!