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?
I think you can do
git rebase -i [hash] [branch_name]
where[hash]
is the identifying hash for however far back you want to rewind plus one (or however many commits back you want to go) and then delete the lines for the commits in the editor that you don't want any more. Save the file. Exit. Pray. And it should be rewound. You might have to do agit reset --hard
, but it should be good at this point. You can also use this to pull specific commits out of a stack, if you don't want to keep them in your history, but that can leave your repository in a state that you probably don't want.If you didn't commit it yet, you can only use
It will undo the merge (and everything that you did).
With newer Git versions, if you have not committed the merge yet and you have a merge conflict, you can simply do:
From
man git merge
:If you want a command-line solution, I suggest to just go with MBO's answer.
If you're a newbie, you might like the graphical approach:
gitk
(from the command line, or right click in file browser if you have that)You could use
git reflog
to find the previous checkout. Sometimes that's a good state you want to return back to.Concretely,
You should reset to the previous commit. This should work:
Or even
HEAD^^
to revert that revert commit. You can always give a full SHA reference if you're not sure how many steps back you should take.In case when you have problems and your master branch didn't have any local changes, you can reset to
origin/master
.