Sub topic 7
Fixing the last commit with --amend
This chapter assumes you can stage files, commit, and read git log. It also assumes you know that the index (the staging area) holds the snapshot for your next commit, and that HEAD points to the current commit on your branch.
What amend does
git commit --amend does not edit a commit. Git commits are immutable, so amend builds a new commit and moves the branch to it. The new commit uses the current index as its snapshot. By default it takes the old commit's message and author. It has the same parent as the old commit, so the history looks as if the old commit never existed.
The old commit still exists in the object database for a while. You can reach it through the reflog, which a later chapter covers.
Add a forgotten file
Suppose you committed but left out a file. Stage it, then amend and keep the message:
git add forgotten.txt
git commit --amend --no-edit--no-edit keeps the existing message and skips the editor. The commit gets a new hash, because its content changed.
Fix the message only
Run amend with nothing staged and you get a new commit with the same files and a new message:
git commit --amend -m "Add retry logic to the upload client"Without -m, Git opens your editor with the old message loaded.
Remove something from the last commit
Amend also works in the other direction. To take a file out of the last commit but keep your edits, unstage it from the previous snapshot, then amend:
git restore --source=HEAD~1 --staged -- config.local
git commit --amend --no-editThe first command sets the index copy of config.local to its state in the parent commit. If the file was newly added in the last commit, the file leaves the commit and stays in your working tree as an untracked file. The second command records the result.
Check the result
Compare the old and new commits before you move on:
git show --stat HEAD
git reflog -3The reflog shows both the amended commit and the one it replaced, which is useful if you amend by mistake.
Common errors
- Amending a pushed commit. The new commit has a different hash from the one on the remote. A plain
git pushis rejected because the histories have diverged. Forcing the push rewrites shared history, so do it only on a branch that nobody else uses, and then usegit push --force-with-lease. A later chapter explains why. If others may have pulled the commit, usegit revertinstead. - Staging more than you meant to. Amend includes everything in the index. Run
git statusfirst, so unrelated changes don't end up in the commit. - Amending the wrong commit. Amend only touches the tip of the current branch. To change an older commit, use interactive rebase.
- Amending during a merge or rebase. In the middle of one of those operations,
HEADmay not be the commit you expect. Finish or abort the operation first.
Summary of when to use it
| Situation | Command |
|---|---|
| Forgot a file | `git add file` then `git commit --amend --no-edit` |
| Typo in message | `git commit --amend -m "new message"` |
| Committed too much | Unstage with `git restore --source=HEAD~1 --staged`, then amend |
| Fix an older commit | `git rebase -i`, not amend |
Exercise
You committed "Add login form" and pushed it to your own feature branch, which nobody else uses. You then notice a missing CSS file. What commands add the file to that commit, and what must you do differently to push the result?
Show answer
Run git add login.css and git commit --amend --no-edit. The amended commit has a new hash, so a normal push is rejected. Push with git push --force-with-lease origin <branch>, which is acceptable here because nobody else uses the branch and the lease check fails if the remote has commits you haven't fetched.
Exercise
After running git commit --amend, how can you find the hash of the commit it replaced?
Show answer
Run git reflog. It lists earlier positions of HEAD, including the original commit just before the amend entry.