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 un
With modern Git, you can:
git merge --abort
Older syntax:
git reset --merge
Old-school:
git reset --hard
But actually, it is worth noticing that git merge --abort
is only equivalent to git reset --merge
given that MERGE_HEAD
is present. This can be read in the Git help for merge command.
git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.
After a failed merge, when there is no MERGE_HEAD
, the failed merge can be undone with git reset --merge
, but not necessarily with git 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.
Got to this question also looking to revert to match origin (ie, NO commits ahead of origin). Researching further, found there's a reset
command for exactly that:
git reset --hard @{u}
Note: @{u}
is shorthand for origin/master
. (And, of course, you need that remote repository for this to work.)
First, make sure that you've committed everything.
Then reset your repository to the previous working state:
$ git reset f836e4c1fa51524658b9f026eb5efa24afaf3a36
or using --hard
(this will remove all local, not committed changes!):
$ git reset f836e4c1fa51524658b9f026eb5efa24afaf3a36 --hard
Use the hash which was there before your wrongly merged commit.
Check which commits you'd like to re-commit on the top of the previous correct version by:
$ git log 4c3e23f529b581c3cbe95350e84e66e3cb05704f
commit 4c3e23f529b581c3cbe95350e84e66e3cb05704f
...
commit 16b373a96b0a353f7454b141f7aa6f548c979d0a
...
Apply your right commits on the top of the right version of your repository by:
By using cherry-pick (the changes introduced by some existing commits)
git cherry-pick ec59ab844cf504e462f011c8cc7e5667ebb2e9c7
Or by cherry-picking the range of commits by:
First checking the right changes before merging them:
git diff 5216b24822ea1c48069f648449997879bb49c070..4c3e23f529b581c3cbe95350e84e66e3cb05704f
First checking the right changes before merging them:
git cherry-pick 5216b24822ea1c48069f648449997879bb49c070..4c3e23f529b581c3cbe95350e84e66e3cb05704f
where this is the range of the correct commits which you've committed (excluding wrongly committed merge).
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:
git checkout develop
git branch -D master
git branch -t master origin/master
Voila! Master is at the same stage as origin, and your mis-merged state is erased.
git stash
git branch -d the_local_branch
git checkout -t <name of remote>
git stash apply
This worked for me..!!
If you notice that you need to revert immediately after the merge and you haven't done anything else after the merge attempt, you can just issue this command:
git reset --hard HEAD@{1}
.
Essentially, your merge sha
will be pointing to HEAD@{0}
if nothing else was committed after the merge and so HEAD@{1}
will be the previous point before the merge.