Amending & Fixing Commits
A typo in your last commit message or a forgotten file doesn't need a messy follow-up commit. git commit --amend lets you fix the most recent commit cleanly — and this lesson also drills the one rule that keeps amending safe: never rewrite history you've already shared.
Learn Amending & Fixing Commits in our free Git course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Git course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn
1️⃣ Fixing the Last Commit Message
Made a typo in your commit message? With nothing staged, git commit --amend -m "new message" rewrites just the message. Because amending replaces the commit, the result has a new hash — it's a fresh commit standing in the old one's place, not an in-place edit.
2️⃣ Adding a Forgotten File
Forgot to include a file, or want one more tweak in the same commit? Stage the change, then run git commit --amend --no-edit . The --no-edit flag keeps the existing message, folding your staged change straight into the last commit — far tidier than a tiny "oops, forgot a file" follow-up.
3️⃣ The Golden Rule: Don't Amend Pushed Commits
Amending rewrites the commit, so only do it on commits you haven't pushed or shared . Once a commit is on a shared remote and someone has pulled it, your amended version (new hash) diverges from their copy. A normal push gets rejected; forcing it can clobber others' work. If a pushed commit needs fixing, prefer git revert , which never rewrites history.
Your turn. Fix the message and fold in a forgotten file on a local commit. Fill in the three blanks.
Common Errors (and the fix)
- Push rejected after amending: you amended a pushed commit, so histories diverged. If it's safe and the branch is yours, git push --force-with-lease ; otherwise revert instead.
- Amend changed more than expected: you had extra files staged. Anything staged gets folded in — check git status before amending.
- Editor opened unexpectedly: you ran --amend without -m or --no-edit . Save and close to keep the message, or use --no-edit next time.
- Amended the wrong commit: amend only touches the latest commit. To fix an older one you need interactive rebase (next lessons).
Pro Tips
- 💡 Amend before you push: polish your last commit while it's still local — it's free and clean.
- 💡 --no-edit is your friend: for "forgot a file" fixes, it keeps the message untouched.
- 💡 Check the hash: a changed hash after amend confirms the rewrite worked.
- 💡 Pushed? Use revert: never rewrite shared history; add a reversing commit instead.
📋 Quick Reference
Command
Purpose
git commit --amend
Rewrite the last commit (opens editor)
git commit --amend -m "msg"
Rewrite just the message
git commit --amend --no-edit
Fold staged changes in, keep message
git log --oneline -1
Confirm the new message and hash
git revert HEAD
Safe undo for an already-pushed commit
git push --force-with-lease
Push a rewritten commit (use with care)
Frequently Asked Questions
Mini-Challenge: Polish Before Pushing
No commands given this time — just the plan. Turn a sloppy first commit into a clean one, ready to push.
🎉 Lesson Complete!
- ✅ git commit --amend rewrites the most recent commit
- ✅ --amend -m fixes the message; --amend --no-edit folds in a file
- ✅ Amending always produces a new hash — it replaces, not edits
- ✅ Never amend a commit you've already pushed and shared
- ✅ For shared commits, git revert is the safe fix
- ✅ Next lesson: mark releases with git tag
Practice quiz
What does do?
- Creates a brand-new commit on top
- Undoes the last commit
- Replaces the most recent commit with a new one
- Pushes to the remote
Answer: Replaces the most recent commit with a new one. --amend rewrites the latest commit, producing a new commit that replaces it.
Which command fixes ONLY the message of the last commit?
- git commit --amend -m "new message"
- git message --edit
- git revert HEAD
- git reset --hard
Answer: git commit --amend -m "new message". With nothing staged, --amend -m just rewrites the commit message.
You forgot to include a file in your last commit. How do you add it?
- git push file
- git stash file
- git revert HEAD
- git add file && git commit --amend --no-edit
Answer: git add file && git commit --amend --no-edit. Stage the file, then amend with --no-edit to fold it into the last commit and keep the message.
What does do with ?
- Cancels the amend
- Keeps the existing commit message unchanged
- Opens the editor
- Deletes the message
Answer: Keeps the existing commit message unchanged. --no-edit reuses the current message instead of opening the editor.
Does amending change the commit's hash?
- Yes — the amended commit is a new commit with a new hash
- No, the hash stays the same
- Only if you change the message
- Only on the remote
Answer: Yes — the amended commit is a new commit with a new hash. Amending rewrites the commit, so it gets a brand-new hash.
Why should you NOT amend a commit you've already pushed and shared?
- It's slower
- It deletes the file
- It rewrites history, so collaborators' copies diverge from yours
- Git forbids it entirely
Answer: It rewrites history, so collaborators' copies diverge from yours. Amending replaces the commit; if others already have the old one, histories conflict.
If you must update an already-pushed amended commit, what do you generally need?
- A normal git push
- A force push (e.g. git push --force-with-lease)
- git pull only
- Nothing special
Answer: A force push (e.g. git push --force-with-lease). Because history was rewritten, a force push is required — and only when it's safe to do so.
Amend only ever affects which commit?
- All commits
- The first commit
- A commit you choose by hash
- The most recent commit (HEAD)
Answer: The most recent commit (HEAD). git commit --amend operates on the latest commit, HEAD.
What happens to staged changes when you run ?
- They're discarded
- They're folded into the rewritten last commit
- They're stashed
- They become a new commit
Answer: They're folded into the rewritten last commit. Anything currently staged is included in the amended commit.
A safer alternative to amending a pushed commit is...
- git reset --hard
- git stash
- git revert
- git clean
Answer: git revert. git revert adds a new commit and doesn't rewrite shared history, so it's safe for pushed work.
Continue this course
- Previous: Exploring History with git log
- Next: Tagging Releases