Fix Last Commit
Git · Example / how-to
Fix Last Commit
Git · Example / how-to
📋 Overview
Amend the most recent commit to fix the message or add forgotten files — only when it is safe (not pushed, or you own the branch).
🔧 Core concepts
| Piece | Role |
|---|---|
git commit --amend | Rewrite HEAD commit |
| Staged changes | Fold into last commit |
| Message edit | --amend -m or editor |
| Safety | Avoid amending published history |
💡 Examples
Fix message only:
git commit --amend -m "fix: correct typo in changelog"Add forgotten files:
git add path/to/missed_file.ts
git commit --amend --no-editCheck before amending:
git status
git log -1 --format='%an %ae %s'
git status -sb # look for "ahead" vs tracking remote⚠️ Pitfalls
- Never amend commits already pushed to a shared branch unless the team agrees (needs force-push).
--no-editkeeps the old message; use an editor/-mwhen the message should change.- Hooks may rewrite files on amend — review
git statusafter.