How can I safely undo a commit?

  • Page Owner: Not Set
  • Last Reviewed: 2019-12-11

I've made a commit to the wrong branch (QA or master) and would like to undo the commit before my next push. How can I do that?


Additional Posts

In these examples a bad commit was made to the QA branch.

Delete the branch and pull fresh

John Recommends: ✔

Using your git tool of choice move off the branch with the bad commit git switch master and delete the bad branch git branch -D qa. The pull down a fresh copy from remote, git fetch origin qa

Note: -d is safe delete (does not orphan commits), and -D is force delete

Git Reset

John Recommends: ✔

Use git reset --hard origin/qa to move your HEAD pointer back to match what is on QA.

Git Revert

John Recommends: ❌

Use git revert <badcommithash> to create a new commit that is the opposite of the bad commit. This adds meaningless history to the repository and could create confusion depending on the severity.