Git push after reset

2019-07-23 05:23发布

问题:

I committed some unnecessary code so I used: git reset HEAD~

I checked out the files and tried to push my last commits (which were correct). However, I'm receiving error: error: failed to push some refs to 'git@github.com:MyProject/MyProject.git' hint: Updates were rejected because the tip of your current branch is behind which is probably since the commit is not reverted correctly since when I do git pull, the commit is back again. How can I correctly revert this commit?

additional info: git status gives me:

Your branch is behind 'origin/Mybranch' by 1 commit, and can be fast-forwarded.

回答1:

If you wish to revert the last commit, do so with git revert HEAD; git push. This creates a revert commit which is pushed on top of the current head.

If you wish to remove the last commit, do so with git reset --hard HEAD^; git push --force. git protects you against rewriting history, so a forced push is needed to remove commits that already exist in the remote.