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?
Okay, the answers other people here gave me were close, but it didn't work. Here's what I did.
Doing this...
...gave me the following status.
I then had to type in the same
git reset
command several more times. Each time I did that, the message changed by one as you can see below.At this point, I saw the status message changed, so I tried doing a
git pull
, and that seemed to work:So long story short, my commands came down to this:
First, make sure that you've committed everything.
Then reset your repository to the previous working state:
or using
--hard
(this will remove all local, not committed changes!):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:
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)
Or by cherry-picking the range of commits by:
First checking the right changes before merging them:
First checking the right changes before merging them:
where this is the range of the correct commits which you've committed (excluding wrongly committed merge).
Lately, I've been using
git reflog
to help with this. This mostly only works if the merge JUST happened, and it was on your machine.git reflog
might return something like:The first line indicates that a merge occurred. The 2nd line is the time before my merge. I simply
git reset --hard 43b6032
to force this branch to track from before the merge, and carry-on.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 fororigin/master
. (And, of course, you need that remote repository for this to work.)You can use only two commands to revert a merge or restart by a specific commit:
git reset --hard commitHash
(you should use the commit that you want to restart, eg. 44a587491e32eafa1638aca7738)git push origin HEAD --force
(Sending the new local master branch to origin/master)Good luck and go ahead!
If you committed the merge: