So long story short I had a remote branch I was working on (my-remote-branch) that was failing some tests in the CI. I was told to rebase to master to fix this issue.
I did the following:
git checkout master
git pull
git checkout my-remote-branch
git rebase master
git push
At that point I got an error:
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
So next I did:
git pull --set-upstream-origin my-remote-branch
git push
This kind of just created a mess. My pull request now has a massive amount of commits (all the ones from master) so the history is all messed up and there's a misleading number of changed files for the pull request. This seems more complicated than a simple revert since I can't just revert back to one commit, all the commits are intertwined from master.
Apparently what I was supposed to do was this:
git fetch origin master
git rebase -I origin/master
git push —force
Is there a way I can undo my mistake (undo the merge, remove commits from history in my branch only) so I can execute the step I was supposed to do?
you can undo the last commit with
git checkout my-remote-branch
git reset --hard HEAD~1
if the last commit wasn't the "bad" one, then you can use git log
to find the last hash of the last good commit an then do
git reset --hard HASH
(replace HASH
with the repective hash).
after that you are back where you started and can do what you think would have been correct.
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.