Git Amend: Edit the Last Commit
- 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.
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.
# 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
Date: Mon Mar 30 14:22:00 2026 +0530
2 files changed, 63 insertions(+)
| Use Case | Command |
|---|---|
| Fix commit message only | git commit --amend -m 'new message' |
| Add forgotten file, keep message | git add <file> && git commit --amend --no-edit |
| Add forgotten file, change message | git add <file> && git commit --amend -m 'new message' |
| Fix author name/email | git commit --amend --author='Name <email>' --no-edit |
| Open editor to rewrite message | git 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.
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.