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-edit

Change the author

git commit --amend --reset-author

Safe force push after amending

git push --force-with-lease

Common 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.

More Git articles