Git amend: rewrite Git history
The cheatsheet
Squashing additional files into the latest commit
git add <file>
git commit --amend --no-edit
Stage the files exactly as you normally would, and --amend folds them into the previous commit instead of creating a new one. The --no-edit flag keeps the existing commit message.
Fixing the latest commit’s message
git commit --amend -m "<message>"
Rewrites just the commit message of the previous commit.
The caveat
Git commits are immutable, meaning they cannot change once created. Because of this, --amend doesn’t actually edit a commit: it replaces it with an entirely new commit and a new hash. On your own local branches that’s fine, but once others have pulled those commits and based work on them, avoid amending.
Going further
Amend only reaches the most recent commit. Altering anything older requires interactive rebase, which I cover in change Git commit messages and delete Git commits, with the written reference in techniques for rewriting Git history.
