简体   繁体   中英

After a git rebase master mistakenly executed git pull instead of git push origin/your_branch -f

Probably due to lack of sleep instead of executing a force push after a git rebase master I've executed a git pull . Now my pull request is messed up including all the commits from master. Any suggestion to revert this?

git reflog has come to the rescue for me on multiple occasions.

This reference log records updates to your local repository/branches/references.

So if you find the head commit of the desired branch directly before the pull in this case, you can reset your current branch to that reference. For example to reset to HEAD@{2}

git reset --hard HEAD@{2}

Note: be absolutely sure about your action before using --hard

Git reflog documentation

Running git pull is just a fetch followed by a merge of the upstream branch. Assuming the merge succeeded and you still have this branch checked out, something like this should fix it:

$ git reset @^1

That resets the branch to the first parent which should be the rebased branch (the second parent being the head of the old upstream version of the branch.) You can verify before doing the reset:

$ git log --graph @^1

or

$ gitk @^1

If those look sane, then the reset should do it. It will not touch the files in your work tree so you can see if this resulted in any surprising changes. I suspect there will be none as the merge was probably a no-op as far as the code is concerned (again, assuming it succeeded.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM