Undo last commit
Keep the changes, drop the commit
Section titled “Keep the changes, drop the commit”Removes the commit but leaves the changes staged in your working tree:
git reset --soft HEAD~1Discard the commit and its changes
Section titled “Discard the commit and its changes”Destructive — the changes are gone from the working tree:
git reset --hard HEAD~1Amend the last commit
Section titled “Amend the last commit”Change the message, or stage more files and fold them in:
git commit --amend -m "New message"# or stage more files, then:git commit --amend --no-editAfter pushing
Section titled “After pushing”Reset and amend rewrite history, so you need to force-push. Always
--force-with-lease over --force — it refuses to overwrite work pushed
by someone else in the meantime:
git push --force-with-leaseReset too hard?
Section titled “Reset too hard?”git reflog keeps every commit HEAD has touched for ~90 days — see
Recover from a bad reset.