The Hook (The "Byte-Sized" Intro)
You just committed. Then you notice: the message has a typo. Or you forgot to add a file. Or you accidentally included a debug log. Don't create a "fix typo" commit — that clutters history. git commit --amend lets you edit the last commit as if it never happened wrong. It's the most-used history rewrite in daily work, and it takes 3 seconds.
📖 What is Fixing the Last Commit?
git commit --amend replaces the most recent commit with a new one. You can change the message, add forgotten files, or remove files that shouldn't have been included. The old commit is replaced (new hash), so only use this before pushing.
Conceptual Clarity
What --amend can fix:
| Problem | Fix |
|---|---|
| Typo in commit message | git commit --amend -m "correct msg" |
| Forgot to add a file | git add file then git commit --amend --no-edit |
| Included a wrong file | git restore --staged file then git commit --amend --no-edit |
| Wrong author | git commit --amend --author="Name <email>" |
Real-Life Analogy
Amending is like peeling a stamp off an envelope, fixing the letter inside, and resealing it — but only before it leaves the mailbox. Once the mail truck picks it up (push), you can't change it without consequences.
Visual Architecture
Why It Matters
- Clean history: No embarrassing "fix typo" or "add forgotten file" commits.
- Speed: The fastest way to fix the most common commit mistakes.
- Professional: Clean commit history shows attention to detail.
- Limitation: Only works on the last commit — use interactive rebase for older commits.
Code
# ─── Fix the commit message ───
git commit --amend -m "Add user authentication module"
# ─── Add a forgotten file (keep same message) ───
git add forgotten-file.js
git commit --amend --no-edit
# ─── Remove a file that shouldn't be committed ───
git restore --staged debug.log
git commit --amend --no-edit
# ─── Change the author ───
git commit --amend --author="Jane Doe <jane@example.com>"
# ─── Open editor to edit message ───
git commit --amend
# Editor opens with current message
# ─── If already pushed, force-push (personal branch only!) ───
git push --force-with-leaseKey Takeaways
--amendreplaces the last commit with a corrected version.- Use
--no-editwhen you only need to change the files, not the message. - Amend creates a new commit hash — only use before pushing to shared branches.
- For older commits, use
git rebase -iinstead of--amend.
Interview Prep
-
Q: What does
git commit --amenddo internally? A: It replaces the last commit with a new one. The old commit gets a new hash because the content or metadata changed. The old commit becomes unreachable but remains in the object database (recoverable via reflog) until garbage collection. -
Q: Is it safe to amend a commit that has been pushed? A: Only if you're the sole developer on that branch. Amending changes the commit hash, so you'll need to force-push. On shared branches, this would break other developers' history.
-
Q: How do you add a forgotten file to the last commit? A:
git add forgotten-file.jsfollowed bygit commit --amend --no-edit. This modifies the last commit to include the file without changing the commit message.