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:

Bash

Now you have unstaged changes + bad last commit.

Step 1 – Stage the forgotten lines

Bash

Step 2 – Amend the last commit

Bash

Two possibilities:

A – You want to change the message too Git opens your default editor (VS Code, vim, nano…) with:

text

Change it to something better:

text

Save & close → Git creates new commit replacing the old one:

text

B – You only want to add the file, keep old message

Bash

→ Same commit hash changes, but message stays the same.

Step 3 – Check what happened

Bash
Bash

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:

Bash

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:
Bash
  • –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! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *