Fixing the last commit
Last updated
A typo in the message, or a file you forgot to add: the last commit is the easiest one to fix. In this lesson you'll use git commit --amend, see that it really makes a new commit in place of the old one, and learn when not to use it.
By the end of this lesson you can
- Change the last commit's message with git commit --amend
- Add a forgotten change to the last commit
- Explain that amend replaces the commit with a new one
- Know not to amend a commit you've already shared
Fix a message
git commit --amend -m "new message" replaces the last commit's message. Nothing else about it changes.
git commit --amendCareful: changes files or history, but Git can usually get it back: it changes history, but only the newest commit, and lesson 2.7 shows how to get the old one back.
Ana just committed the halo-halo recipe with a typo in the message. Commit it, then fix the message.
- Make a file
halo-halo.md(tryecho "# Halo-halo" > halo-halo.md) - Commit it with the message
"Add hal-halo recipe"(typo on purpose) - Fix the message to
"Add halo-halo recipe"with--amend
Working folder
no changes
Staging area
nothing staged
Repository
4 commits on main
You are in ~/kusina, which has four commits.
Commit graph
1c17fadHEAD →main Add a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Add what you forgot
Forgot a file? Stage it and amend. --no-edit keeps the message as it was:
git add ube.md
git commit --amend --no-editThe last commit now holds both files, as if you'd added them together the first time.
- Commit a new
halo-halo.mdwith a good message - Make
ube.md, then add it to the same commit with--amend --no-edit
Working folder
no changes
Staging area
nothing staged
Repository
4 commits on main
You are in ~/kusina, which has four commits.
Commit graph
1c17fadHEAD →main Add a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
What amend really does
A commit can never be changed: its hash is made from its contents, so a changed commit would be a different commit. --amend makes a new commit with the new message or contents and the same parent, moves the branch to it, and leaves the old one behind.
$ git add halo-halo.md $ git commit -m "Add hal-halo recipe" [main 9ab1d1c] Add hal-halo recipe 1 file changed, 1 insertion(+) create mode 100644 halo-halo.md
Working folder
no changes
Staging area
nothing staged
Repository
5 commits on main
9ab1d1cHEAD →main Add hal-halo recipe1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Spot the trap: amending a shared commit
Amending is fine for commits only you have. Once you've shared a commit (Stage 4: pushed it), someone else may have it and build on it.
Ana pushes “Add pancit recipe” to the shared repository. Joaquin pulls it. Then Ana notices a typo, amends the commit, and pushes again.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-commit, --amend: replacing the tip of the current branch with a new commit
- Pro Git: Undoing Things: amending, and only amending commits that are still local
- Git documentation: git-commit, --no-edit: amending without changing the message