Amending & Fixing Commits
Reviewed & published by Brayan K
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.
Part of the free Git course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.
What You'll Learn
- Rewrite the last commit with git commit --amend
- Fix only the message with --amend -m
- Fold in a forgotten file with --amend --no-edit
- Understand that amending creates a new hash
- Know exactly when NOT to amend (pushed commits)
- Pick revert as the safe alternative for shared commits
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.
# Forgot to include a file (or made one more tweak) in the last commit?
# Stage it, then fold it into that commit WITHOUT changing the message.
# 1) Stage the forgotten file / extra change
git add forgotten.js
# 2) Amend, reusing the existing message (--no-edit skips the editor)
git commit --amend --no-edit
# The last commit now contains BOTH files, message unchanged:
git show --stat HEAD3️⃣ 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.
# ⚠️ THE GOLDEN RULE: only amend commits you have NOT pushed/shared.
# Amending rewrites the commit (new hash), so anyone who already pulled
# the old commit now has a different history from you.
# SAFE — local, unpushed commit:
git commit --amend -m "Better message"
# DANGEROUS — the commit is already on origin/main:
# git push # rejected: histories diverged
# git push --force-with-lease # only if you're SURE no one relies on it
# Pushed already and others have it? Don't amend — use revert instead:
git revert HEADYour turn. Fix the message and fold in a forgotten file on a local commit. Fill in the three blanks.
# 🎯 YOUR TURN — replace each ___ then run it in your terminal.
# Goal: fix your most recent (local, unpushed) commit.
# 1) Oops, the message had a typo — rewrite just the message
git commit ___ -m "Add search box" # 👉 the flag that rewrites the last commit
# 2) You also forgot a file — stage it, then fold it in keeping the message
git add search.css
git commit --amend ___ # 👉 the flag that keeps the message
# 3) Confirm the commit now has the new message AND both files
git show --stat HEAD
# ✅ Expected: one commit titled "Add search box" containing both files,
# with a NEW hash (the original was replaced).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) |
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.
# 🎯 MINI-CHALLENGE: polish a sloppy commit before pushing.
# No commands given — just the plan. Run each step in your terminal.
#
# 1. Commit a file with a deliberately bad message ("wip stuff")
# 2. Notice you forgot a second file — stage it
# 3. Amend so the commit contains BOTH files and a clear message
# 4. Run "git log --oneline -1" and confirm the new message + new hash
# 5. Explain to yourself: why would step 3 be UNSAFE if you'd already pushed?
#
# ✅ Expected: a single clean commit, ready to push for the first time.
# your commands here🎉 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
Practice quiz
What does `git commit --amend` 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 `--no-edit` do with `git commit --amend`?
- 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 `git commit --amend`?
- 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 — Mark versions with lightweight and annotated tags, and semantic versioning
- Quick reference: Git cheat sheet › Undo & Rescue
Frequently asked questions
What exactly does git commit --amend do?
It replaces your most recent commit (HEAD) with a new one that combines the old commit's contents, whatever is currently staged, and (optionally) a new message. It's perfect for fixing a typo in the message or folding in a file you forgot. Importantly, it doesn't edit the old commit in place — it creates a brand-new commit with a new hash that takes the old one's spot.
How do I fix just the commit message?
With nothing staged, run git commit --amend -m "corrected message". Because there are no staged changes, the only thing that changes is the message. If you omit -m, Git opens your editor pre-filled with the old message so you can edit it. Either way, the result is a new commit replacing the previous one.
How do I add a forgotten file to my last commit?
Stage the file with git add, then run git commit --amend --no-edit. The --no-edit flag reuses the existing message, so the file is folded into the last commit without prompting you to rewrite the message. This is much tidier than making a tiny follow-up commit like 'oops, forgot a file'.
Why is amending a pushed commit dangerous?
Amending rewrites the commit, giving it a new hash. If you've already pushed the original and a teammate pulled it, their history still contains the old commit while yours has the replacement — the two diverge. Pushing will be rejected, and forcing it can overwrite or confuse others' work. The rule is simple: only amend commits that are still local and unpushed.
I already pushed and need to fix it — now what?
If the commit is shared, prefer git revert, which adds a new commit undoing the change without rewriting history. If you genuinely must amend a pushed commit (e.g. on a private branch only you use), you'll need a force push such as git push --force-with-lease, which checks the remote hasn't changed under you. Never force-push a branch others actively work on.