Homeβ€Ί DevOpsβ€Ί Git Amend: Edit the Last Commit

Git Amend: Edit the Last Commit

Where developers are forged. Β· Structured learning Β· Free forever.
πŸ“ Part of: Git β†’ Topic 19 of 19
Learn how to use git commit --amend to fix the last commit message, add forgotten files, or change staged content.
πŸ§‘β€πŸ’» Beginner-friendly β€” no prior DevOps experience needed
In this tutorial, you'll learn:
  • git commit --amend rewrites the most recent commit β€” use it for typo fixes, forgotten files, or author corrections before pushing.
  • --no-edit keeps the existing commit message when you only want to change the content.
  • Amend only commits that haven't been pushed, or that are exclusively on your own branch. For shared branches, git revert is always the safer choice.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
⚑ Quick Answer
git commit --amend is the 'edit last message' button for Git. You just committed and immediately noticed a typo in the message, or you forgot to include one file. Amend lets you rewrite that last commit β€” same changes, different message or different files β€” before anyone else has seen it.

The --amend flag is one of the first shortcuts every developer memorises. Commit, realise the message says 'fxi' instead of 'fix', amend. It's mechanical and fast. The failure mode is also fast: amending a commit that's already on a shared remote branch rewrites history and forces a --force push. The test is simple: has anyone pulled this commit? If yes, revert; don't amend.

Amend the Last Commit

There are two amend patterns: changing the message only, or changing the message and the content.

For message-only: git commit --amend -m 'new message'. No editor, one line.

For content changes: stage the additional files or changes first, then amend. The staged changes get rolled into the existing commit. The result is one commit with the new content and your new message.

git_amend.sh Β· BASH
12345678910111213141516171819
# Fix typo in last commit message (no editor)
git commit --amend -m "feat(payment): Add PaymentRetryService with exponential backoff"

# Fix message with editor (opens your configured editor)
git commit --amend

# Add a forgotten file to the last commit
git add src/main/java/io/thecodeforge/payment/RetryConfig.java
git commit --amend --no-edit   # --no-edit keeps the existing message

# Change staged content AND message
git add src/main/java/io/thecodeforge/payment/RetryConfig.java
git commit --amend -m "feat(payment): Add PaymentRetryService and RetryConfig"

# Amend author information
git commit --amend --author="Naren <naren@thecodeforge.io>" --no-edit

# After amend: force push to update remote (ONLY on your own branch)
git push origin feature/payment-retry --force-with-lease
β–Ά Output
[feature/payment-retry 9f2c4a1] feat(payment): Add PaymentRetryService with exponential backoff
Date: Mon Mar 30 14:22:00 2026 +0530
2 files changed, 63 insertions(+)
⚠️
Never amend a commit on a shared branchgit commit --amend rewrites the last commit β€” it creates a new commit object with a different hash. If you've pushed this commit to a remote branch that teammates are working from, amending and force-pushing will cause their local histories to diverge. The rule: amend only commits that haven't been pushed, or that are on a branch only you use. For anything on main, develop, or a PR that reviewers have already fetched β€” use git revert instead.
Use CaseCommand
Fix commit message onlygit commit --amend -m 'new message'
Add forgotten file, keep messagegit add <file> && git commit --amend --no-edit
Add forgotten file, change messagegit add <file> && git commit --amend -m 'new message'
Fix author name/emailgit commit --amend --author='Name <email>' --no-edit
Open editor to rewrite messagegit commit --amend

🎯 Key Takeaways

  • git commit --amend rewrites the most recent commit β€” use it for typo fixes, forgotten files, or author corrections before pushing.
  • --no-edit keeps the existing commit message when you only want to change the content.
  • Amend only commits that haven't been pushed, or that are exclusively on your own branch. For shared branches, git revert is always the safer choice.
  • After amending a pushed commit, you must force push with --force-with-lease.

⚠ Common Mistakes to Avoid

  • βœ•Amending a commit that was already pushed to a shared branch β€” requires force push, breaks teammates' local histories.
  • βœ•Using --amend when you meant to create a new commit β€” amend rewrites history. If the change is logically separate, it deserves its own commit.
  • βœ•Forgetting to force push after amending a pushed commit β€” your local and remote branches now have different history, and normal git push will be rejected.

Interview Questions on This Topic

  • QWhat does git commit --amend do and when should you NOT use it?
  • QYou pushed a commit with a typo in the message. What are your options for fixing it?

Frequently Asked Questions

Does git commit --amend create a new commit?

Yes. Amend creates a new commit object with a new hash, even if the content is identical. The old commit is no longer referenced by the branch. This is why amending pushed commits requires a force push β€” the remote still has the old commit hash.

How do I amend a commit message without opening an editor?

Use git commit --amend -m 'your new message'. The -m flag provides the message inline so no editor is opened.

How do I add a forgotten file to the last commit?

Stage the file with git add <filename>, then run git commit --amend --no-edit. The --no-edit flag preserves the existing commit message.

πŸ”₯
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousGit Fetch vs Pull: What's the Difference
Forged with πŸ”₯ at TheCodeForge.io β€” Where Developers Are Forged