Lesson Completion
Back to course

git commit --amend

Beginner
9 minutes4.7Git

The Hook (The "Byte-Sized" Intro)

You just committed and then noticed a typo in the message. Or you forgot to add that one file. Or you left a console.log in. Before Git, you'd create a shameful "fix typo" commit. With --amend, you rewrite the last commit as if the mistake never happened. It's the Ctrl+Z for your most recent commit — clean, simple, one command.

📖 What is git commit --amend?

git commit --amend replaces the most recent commit with a new one. You can use it to fix the commit message, add forgotten files, or remove accidental changes — all without creating extra commits in your history.

Conceptual Clarity

  • --amend does not edit the existing commit — it creates a brand-new commit that replaces the old one
  • The old commit gets a new SHA-1 hash (because the content or metadata changed)
  • You can amend the message only, the content only, or both
  • Rule: Never amend a commit that's already been pushed and shared. It rewrites history, which causes problems for teammates.

What happens internally:

Before amend: A --- B (HEAD) After amend: A --- B' (HEAD) ← B' is a new commit; old B is orphaned

Real-Life Analogy

It's like crossing out a line in a sealed envelope, rewriting it, and resealing the envelope with a new stamp. To anyone who checks later, it looks like the original letter was always correct. But if you already mailed the letter (pushed), the recipient has the old version — and now there's a conflict.

Visual Architecture

flowchart LR A["📸 Commit A"] --> B["📸 Commit B<br/>(original)"] B -->|"git commit --amend"| B2["📸 Commit B'<br/>(replaced)"] B -.->|"orphaned"| X["🗑️ Old B"] style A fill:#1a1a2e,stroke:#e94560,color:#e94560 style B fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style B2 fill:#0f3460,stroke:#53d8fb,color:#53d8fb style X fill:#2d1b1b,stroke:#e94560,color:#e94560

Why It Matters

  • Clean history: No "fix typo" or "forgot file" commits cluttering your log.
  • Professional commits: Every commit in your history is intentional and complete.
  • PR quality: Reviewers see one clean commit instead of a commit + fixup pair.
  • One restriction: Don't amend after pushing — it forces teammates to deal with rewritten history.

Code

bash
# ─── Fix a typo in the commit message ─── git commit --amend -m "Add user authentication middleware" # Replaces the last commit message (content stays the same) # ─── Add a forgotten file to the last commit ─── git add forgotten-file.js git commit --amend --no-edit # --no-edit keeps the original message; just adds the file # ─── Change both message and content ─── git add extra-fix.js git commit --amend -m "Add auth middleware with rate limiting" # ─── Amend the author (useful after misconfiguring identity) ─── git commit --amend --author="Nikhil Gundala <nikhil@example.com>" --no-edit # ─── Open editor to review and edit the full message ─── git commit --amend # Opens your configured editor with the current message preloaded # ─── Verify the amended commit ─── git log --oneline -2 # Output: # f1g2h3i (HEAD -> main) Add auth middleware with rate limiting # a1b2c3d Previous commit # Note: the hash changed because it's a new commit object

When to Use vs When to Avoid

✅ Safe to Amend❌ Don't Amend
Commit is only local (not pushed)Commit already pushed to a shared branch
Fixing a typo in the messageTeammates have pulled the original commit
Adding a forgotten fileThe branch has active PRs or CI based on it
Removing a debug statementYou're not sure who else has the commit
bash
# ⚠️ If you already pushed and MUST amend: git commit --amend -m "Fixed message" git push --force-with-lease # --force-with-lease is safer than --force: it fails if someone else pushed

Key Takeaways

  • --amend replaces the last commit with a corrected version — new hash, clean history.
  • Use --no-edit when adding files without changing the message.
  • Never amend pushed commits unless you're working solo or your team explicitly agrees.
  • If you must force-push an amend, use --force-with-lease instead of --force.

Interview Prep

  • Q: What does git commit --amend do? A: It replaces the most recent commit with a new one. You can modify the message, add/remove files, or both. The old commit is orphaned and eventually garbage collected.

  • Q: Why does the commit hash change after amending? A: Because the SHA-1 hash is generated from the commit's contents (tree, parent, author, message, timestamp). Changing any of these inputs produces a different hash, making it a completely new commit object.

  • Q: When should you NOT use --amend? A: Never amend a commit that has already been pushed to a shared branch. Other developers may have based their work on the original commit. Amending rewrites history, which forces them to reconcile the conflict with git pull --rebase or similar.

Topics Covered

Git BasicsGit Fundamentals

Tags

#git#amend#commit#beginner-friendly

Last Updated

2026-02-12