Chapter 38: Git Amend
Git commit –amend (how to fix / improve / replace the very last commit you just made)
This is one of the most frequently used “undo/fix” commands in everyday Git work — especially when you’re working alone or on a personal branch that hasn’t been pushed yet.
Many beginners discover –amend the hard way: they commit something, immediately realize “oh no, I forgot a file” or “the message is terrible”, and then panic because they think they have to live with it forever.
Today I’m going to explain git commit –amend very clearly and slowly — like I’m sitting next to you watching the terminal — with real examples, exact outputs, the mental model you need, and all the safety rules so you never lose work.
1. What does git commit –amend really do? (simple explanation)
git commit –amend replaces the most recent commit with a new commit that has:
- the same parent (so the commit before it stays the same)
- your current staged changes (what is in the index right now)
- a new message (you can keep the old one or write a better one)
- a new commit hash (old hash disappears from normal view)
In other words:
- It undoes the last commit but keeps its changes in the staging area
- Then immediately re-commits with whatever is currently staged + whatever message you want
Think of it like:
You just sent an email with a typo in the subject and forgot the attachment. Instead of sending a second email saying “sorry, correction”, you recall the email, fix subject + add attachment, and send a corrected version — the receiver sees only the good one.
2. The two most common use-cases (why 95% of people use –amend)
Use-case A – Forgot to stage a file / made typo in message
You committed but forgot one file or wrote a bad message.
Use-case B – Small fix right after commit
You committed, ran tests → found tiny bug → fixed it → want to fold the fix into the previous commit instead of creating noisy “fix typo” commit.
3. Realistic Example – Step by Step (copy-paste ready)
Let’s create a small playground repo:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
mkdir git-amend-demo cd git-amend-demo git init echo "# Todo App" > README.md echo "- Buy milk" >> README.md git add README.md git commit -m "Add initial todo list" # Oops – forgot to add "Call mom" and message is too vague echo "- Call mom" >> README.md echo "- Walk dog" >> README.md |
Now you have unstaged changes + bad last commit.
Step 1 – Stage the forgotten lines
|
0 1 2 3 4 5 6 |
git add README.md |
Step 2 – Amend the last commit
|
0 1 2 3 4 5 6 |
git commit --amend |
Two possibilities:
A – You want to change the message too Git opens your default editor (VS Code, vim, nano…) with:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Add initial todo list # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Fri Feb 13 18:45:00 2026 +0530 # # On branch main # Changes to be committed: # modified: README.md |
Change it to something better:
|
0 1 2 3 4 5 6 7 8 9 10 |
feat: initialize todo list with first three items - Buy milk - Call mom - Walk dog |
Save & close → Git creates new commit replacing the old one:
|
0 1 2 3 4 5 6 7 8 |
[main def5678] feat: initialize todo list with first three items Date: Fri Feb 13 18:45:00 2026 +0530 1 file changed, 3 insertions(+) |
B – You only want to add the file, keep old message
|
0 1 2 3 4 5 6 |
git commit --amend --no-edit |
→ Same commit hash changes, but message stays the same.
Step 3 – Check what happened
|
0 1 2 3 4 5 6 7 8 |
git log --oneline -2 # def5678 feat: initialize todo list with first three items # (no older commit visible anymore) |
|
0 1 2 3 4 5 6 7 |
git show # you see the new commit includes all three lines |
Step 4 – If you already pushed? (danger zone)
If you did git push before amending:
- Never amend + force-push on shared branch (main, develop)
- It changes commit hash → breaks teammates who pulled
Safe alternatives:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Option A – keep history honest git revert HEAD git commit -m "fix: oops – revert bad commit" git push # Option B – if it's your personal branch & team agrees git commit --amend git push --force-with-lease origin your-branch |
4. Important Rules & Safety Tips (2026 edition)
- Never amend on commits that other people already pulled/pushed → Use git revert instead
- –amend only affects the very last commit → Cannot fix commits from 3 days ago with this
- If you amend and regret it — use reflog:
|
0 1 2 3 4 5 6 7 8 |
git reflog # shows old commit hash before amend git reset --hard HEAD@{1} # go back to before amend |
- –no-edit is your friend when you only want to add forgotten files
- Many teams use commit message conventions → amending is perfect for fixing to match style
5. Quick git commit –amend Cheat Sheet
| What you want to do | Command | Notes |
|---|---|---|
| Amend + change message | git commit –amend | Opens editor — best for fixing message |
| Amend + keep old message | git commit –amend –no-edit | Fast — only adds staged files |
| Amend + new message in one line | git commit –amend -m “new better message” | Quick when you know what to write |
| Add forgotten file to last commit | git add forgotten-file.js git commit –amend –no-edit | Most common daily use |
| Recover after bad amend | git reflog → git reset –hard HEAD@{1} | Safety net — reflog keeps old state ~90 days |
Got the –amend feeling now?
git commit –amend = “replace the last commit with an improved version — same parent, new hash, new message, new snapshot”
It’s your daily “oops, I forgot something” button — but only safe before pushing.
Next?
- Want to see amend + interactive rebase combo for cleaning multiple commits?
- How to handle “I amended and now push fails”?
- Or back to revert / reset comparison?
Just tell me — we’ll continue the class. You’re becoming very strong with Git — really proud of you! 🚀
