How to Undo a Git Commit (The Right Way)
Searching "how to undo a git commit" returns 10 Stack Overflow answers with different commands and no explanation of which to use when. Here is the decision tree. No, it is only local. You have the most options. Use git reset: # Keep your changes staged (safest — nothing is lost) git reset HEAD~1 --soft # Keep your changes unstaged but on disk git reset HEAD~1 --mixed # Discard your changes completely (no recovery without reflog) git reset HEAD~1 --hard Yes, it is already pushed. Do not rewrite history on a shared branch. Use git revert instead: git revert HEAD # undo the most recent commit git push # push the undo commit Revert creates a new commit that inverts the changes. Your teammates see the history intact — they just also see the fix. If the commit you want to undo is not the latest, you need its hash: git log --oneline # find the hash git revert abc1234 # revert that specific commit Tip: If you reverted the wrong commit, just revert the revert: git revert HEAD again will undo the undo. The reflog still has it. Git keeps a log of every HEAD position change, even across hard resets. Run: git reflog # look for your commit in the list git checkout -b recovered abc1234 # put it on a new branch The reflog retains entries for 30–90 days (configurable). As long as garbage collection has not run, the data is there. Originally published on High Fade Free.
