Amending Commits

When you have just created a commit and discover a small mistake, git commit --amend can replace the current commit with a corrected version. It is useful for fixing the latest commit message or adding forgotten changes.

Amending changes the commit identity. Avoid rewriting a commit that has already been shared when other developers may depend on its original history.

Amend the Latest Commit
git add forgotten-file.js
git commit --amend --no-edit

git commit --amend -m "Correct commit message"

--no-edit keeps the existing message. Supplying -m replaces it. Both operations create a replacement commit for the previous HEAD.

Key Takeaways
  • Amend is ideal for fixing the latest local commit.

  • Amending changes the commit hash.

  • Be careful when rewriting history that others have already pulled.

Knowledge Check

What is a common use for git commit --amend?