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
- Amend the commit message:
git commit --amend -m "New message"
- Add forgotten files:
git add forgotten.txt
git commit --amend --no-edit
- Amend and keep the message in your editor:
git commit --amend
Tips
- Only amend commits that have not been pushed to shared branches.
--no-editpreserves 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.