Reputation: 1090
I'm in this situation where I accidentally merged a branch into master
.
Now, I want the master
branch to get back to its previous state. I used git reset --hard HEAD^
but when I want to push I get this error:
Updates were rejected because the tip of your current branch is behind its remote counterpart
Is there any way I can fix this?
Upvotes: 0
Views: 978
Reputation: 41
As I understand, you are currently working on the master branch and simply want to reset it to be exactly like its remote counterpart (i.e., discarding your changes). The simplest way to do this is:
$ git fetch
$ git reset --hard origin/master
If you've already pushed the merge that you made, you will have to do then have to do a force push. Be careful, however, since that will wipe out any changes that others had potentially made to master in the meantime.
$ git reset --hard <hash of commit that is the earlier version of master to which you want to revert, as shown in the merge commit that you made>
$ git push -f
Upvotes: 2
Reputation: 142552
You have several options:
delete the local branch and check it out again:
# checkout temp branch
git checkout - b tempBranch
# delete the current branch
git branch -D master
# checkout the old branch
git fetch --all --prune
git checkout master
use the git reflog
git reflog
You can always use the reflog
as well.
git reflog
will display any change which updated the HEAD
and checking out the desired reflog entry will set the HEAD
back to this commit.
Every time the HEAD is modified there will be a new entry in the reflog
git reflog
git checkout HEAD@{...}
This will get you back to your desired commit
This will put you in a detached head and you will have to checkout new branch and follow the previous step as described in step #1.
Upvotes: 1