Within my master branch, I did a git merge some-other-branch
locally, but never pushed the changes to origin master. I didn't mean to merge, so I'd like to undo it. When doing a git status
after my merge, I was getting this message:
# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
Based upon some instructions I found, I tried running
git revert HEAD -m 1
but now I'm getting this message with git status
:
# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.
I don't want my branch to be ahead by any number of commits. How do I get back to that point?
The simplest answer is the one given by odinho - Velmont
First do
git reset --merge ORIG_HEAD
For those looking to reset after changes are pushed, do this (Because this is the first post seen for any git reset merge questions)
git push origin HEAD --force
This will reset in a way that you won't get the merged changes back again after pull.
It is strange that the simplest command was missing. Most answers work, but undoing the merge you just did, this is the easy and safe way:
The ref
ORIG_HEAD
will point to the original commit from before the merge.(The
--merge
option has nothing to do with the merge. It's just likegit reset --hard ORIG_HEAD
, but safer since it doesn't touch uncommitted changes.)Assuming your local master was not ahead of origin/master, you should be able to do
Then your local
master
branch should look identical toorigin/master
.If your merge and the corresponding commits were not pushed yet, you can always switch to another branch, delete the original one and re-create it.
For example, I accidentally merged a develop branch into master and wanted to undo that. Using the following steps:
Voila! Master is at the same stage as origin, and your mis-merged state is erased.
You can use the git-reset command.
GIT-Reset
With modern Git, you can:
Older syntax:
Old-school:
But actually, it is worth noticing that
git merge --abort
is only equivalent togit reset --merge
given thatMERGE_HEAD
is present. This can be read in the Git help for merge command.After a failed merge, when there is no
MERGE_HEAD
, the failed merge can be undone withgit reset --merge
, but not necessarily withgit merge --abort
, so they are not only old and new syntax for the same thing.Personally I find
git reset --merge
much more powerful and useful in everyday work, so that's the one I always use.