It depends on what you want and whether you have pushed.
Not pushed yet:
Fix only the message:git commit --amend -m "Correct message"
Undo the commit, keep the changes staged:git reset --soft HEAD~1
Undo the commit, keep the changes unstaged:git reset HEAD~1
Undo the commit and throw the changes away:git reset --hard HEAD~1 (be careful, this deletes work)
Already pushed to a shared branch:
Use git revert <commit-id>. It creates a new commit that reverses the old one, so nobody's history is rewritten.
For the wrong-branch case: run git reset --soft HEAD~1, switch to the right branch with git switch right-branch, and commit again. If you panic, git reflog shows where HEAD has been, and you can usually recover a "lost" commit from there.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
It depends on what you want and whether you have pushed.
Not pushed yet:
git commit --amend -m "Correct message"git reset --soft HEAD~1git reset HEAD~1git reset --hard HEAD~1(be careful, this deletes work)Already pushed to a shared branch:
git revert <commit-id>. It creates a new commit that reverses the old one, so nobody's history is rewritten.For the wrong-branch case: run
git reset --soft HEAD~1, switch to the right branch withgit switch right-branch, and commit again. If you panic,git reflogshows where HEAD has been, and you can usually recover a "lost" commit from there.