How to amend a Git commit

· Category: Git

Short answer

Use git commit --amend to modify the most recent commit's message or contents.

Steps

  1. Amend the commit message:
git commit --amend -m "New message"
  1. Add forgotten files:
git add forgotten.txt
git commit --amend --no-edit
  1. Amend and keep the message in your editor:
git commit --amend

Tips

  • Only amend commits that have not been pushed to shared branches.
  • --no-edit preserves the original message while updating contents.
  • Amending changes the commit hash.

Common issues

  • Amending pushed commits requires force push, which disrupts collaborators.
  • Interactive rebase is better for amending older commits.