How to Edit the Last Commit in Git (git commit --amend)
git commit --amend replaces the most recent commit with a new one, letting you fix its message or add forgotten changes.
Published September 27, 2026
git commit --amend replaces the most recent commit, letting you fix its message or add forgotten changes. It rewrites history, so avoid it on commits already pushed and shared.
How it works
Amend creates a brand-new commit with a new hash and discards the old one. Staged changes are folded into the amended commit.
If the commit was already pushed, you need a force push, which can disrupt teammates.
Try it
git add forgotten.js
git commit --amend --no-edit
[main 9a8c7d] Add search endpoint
Practical examples
Fix the message
git commit --amend -m "Correct message"Add a forgotten file
git add file.js
git commit --amend --no-editChange the author
git commit --amend --reset-authorSafe force push after amending
git push --force-with-leaseCommon mistakes
- Amending a commit that teammates already pulled.
- Using plain --force instead of --force-with-lease.
- Amending when you meant to make a new commit.
Key takeaways
- Amend rewrites the last commit.
- Only before sharing.
- Use --force-with-lease if you must push.
FAQ
Does it change older commits?
No, only the most recent one.
Can I undo an amend?
Yes, find the old commit with git reflog.