How to undo (almost) anything with Git
Fix the last commit message
Scenario: You just typo’d the last commit message, you did
git commit -m "Fxies bug #42"
but beforegit push
you realized that really should say “Fixes bug #42”.Undo with:
git commit --amend
orgit commit --amend -m "Fixes bug #42"
What’s happening:
git commit --amend
will update and replace the most recent commit with a new commit that combines any staged changes with the contents of the previous commit. With nothing currently staged, this just rewrites the previous commit message.Undo “local” changes
Scenario: The cat walked across the keyboard and somehow saved the changes, then crashed the editor. You haven’t committed those changes, though. You want to undo everything in that file—just go back to the way it looked in the last commit.
Undo with:
git checkout -- <bad filename>