Author:
Cancel Push⚓︎
We just commit + push changes to remote repo by mistake. How to cancel the push? (Collaborative situation)
There are two scenarios:
- Cancel Push and "Discard" Changes Record.
- Cancel Push but "Keep" Changes Record.
Cancel Push and "Discard" Changes Record⚓︎
In this scenario, we want the remote repo be like never have been pushed these changes.
terminal
git reset HEAD~1 # Undo last commit locally
git status # Check status, you will see the last changes are unstaged
git push --force # Force push to remote to overwrite history
Warning
Force pushing can overwrite history and affect other collaborators. For example, if someone else has pulled the changes, they may encounter conflicts. USE WITH CAUTION.
Cancel Push but "Keep" Changes Record⚓︎
In this scenario, we want to remove the commit from the remote repo but keep the changes locally. You can edit the changes and recommit later.
terminal
git reset --soft HEAD~1 # Undo last commit locally but keep changes staged
git push --force # Force push to remote to overwrite history
--soft option keeps your changes in the staging area.