Amending Commits
Just committed and spotted a typo in the message, or forgot to add a file? git commit --amend replaces the most recent commit with a corrected one. It's perfect for tidying up work you haven't shared yet.
Fixing the message
echo "Omelette: eggs, butter" > omelette.txt git add omelette.txt git commit -m "Add omlette recpie"
[main f5d9ff3] Add omlette recpie 1 file changed, 1 insertion(+) create mode 100644 omelette.txt
git commit --amend -m "Add omelette recipe"
git log --oneline
[main 2f2bdf6] Add omelette recipe Date: Mon Mar 2 09:28:00 2026 +0000 1 file changed, 1 insertion(+) create mode 100644 omelette.txt 2f2bdf6 (HEAD -> main) Add omelette recipe c4f5e47 Add pancake recipe
The Date: line is a reminder that an amended commit keeps the original author date. Without -m, --amend opens your editor with the old message ready to edit.
Adding a forgotten file
Sam meant to include a shopping list with the omelette commit. Stage it, then amend; --no-edit keeps the existing message:
echo "eggs, butter" > shopping-list.txt
git add shopping-list.txt
git commit --amend --no-edit
[main 933c98d] Add omelette recipe Date: Mon Mar 2 09:28:00 2026 +0000 2 files changed, 2 insertions(+) create mode 100644 omelette.txt create mode 100644 shopping-list.txt
The same works for a change you forgot to stage in a file that's already part of the commit.
Amending creates a new commit
Compare the hashes: each amend produced a different one. Git can't edit commits — a commit's hash depends on its contents, so any change makes a new commit. --amend builds a new commit from the old one's parent, moves the branch to it, and leaves the old commit behind (it's still reachable through the reflog for a while; lesson 20).
before: A ── B ◀ main after: A ── B' ◀ main
╲
B (no longer on any branch)
Don't amend pushed commits
Because amending replaces a commit, it rewrites history. If you've already pushed the original commit and someone else has pulled it, their history and yours now disagree, and Git will refuse your next normal push. The rule of thumb:
- Not pushed yet — amend freely.
- Pushed to your own feature branch that no one else uses — you can amend and then
git push --force-with-lease(lesson 32). - Pushed to a shared branch like
main— don't. Make a new commit with the fix, or usegit revert(lesson 19).
Changing the author or date
If you committed with the wrong name or email — for example before configuring Git on a new laptop — fix your configuration, then reset the commit's author to your current settings:
git config user.email "sam.rivera@example.org" git commit --amend --no-edit --reset-author git log -1 --format="%h %an <%ae>"
[main 8227a24] Add omelette recipe 2 files changed, 2 insertions(+) create mode 100644 omelette.txt create mode 100644 shopping-list.txt 8227a24 Sam Rivera <sam.rivera@example.org>
You can also set it explicitly with --author="Name <email>", and update just the date with --date=now.
Fixing older commits
--amend only touches the latest commit. To fix one further back, create a "fixup" commit and fold it in with an interactive rebase — both are covered in lesson 27:
git commit --fixup=a1b2c3d git rebase -i --autosquash a1b2c3d~1
Try it yourself
Make a commit with a deliberately misspelled message and one file missing. Fix both with a single git commit --amend, and compare the hash before and after.