Have you ever made a commit in Git and realized you forgot to include a file or made a typo in the commit message? git commit --amend
is a powerful command that lets you fix mistakes in your most recent commit.
What Does git commit --amend
Do?
The --amend
flag allows you to:
- Edit the last commit message
- Add new changes to the last commit
- Combine staged changes with the previous commit
This is especially useful before pushing changes to a remote repository, as it helps keep your commit history clean.
Basic Usage & Examples
1. Changing the Last Commit Message
If you made a typo in your commit message, you can fix it with:
git commit --amend -m "New and improved commit message"
2. Adding Forgotten Files to the Last Commit
Suppose you made a commit but forgot to include a file:
git add forgotten_file.txt
git commit --amend --no-edit # Keeps the original message
The --no-edit
flag ensures the commit message remains unchanged.
3. Modifying Both Commit Message & Content
If you want to update both the commit message and include new changes:
git add new_changes.txt
git commit --amend -m "Updated feature with new changes"
Tips and Tricks
1. Amending Merge Commits (Carefully!)
Normally,
--amend
works on regular commits, but you can amend a merge commit (fromgit merge
).However, this can get messy because merge commits have multiple parents.
How?
git merge feature-branch --no-commit # Pause before committing
git commit --amend -m "Better merge message"
- ⚠ Warning: Rewriting merge commits can break branch history.
2. Amending Without Changing Timestamp
By default,
--amend
updates the commit timestamp.To keep the original timestamp (useful for CI/CD or audits):
git commit --amend --no-edit --date="$(git log -1 --format=%aD)"
- Or set a custom date:
git commit --amend --date="2023-01-01T12:00:00"
3. Amending the Root Commit (First Commit in Repo)
The very first commit (
git init
) can also be amended!But since it has no parent, you'll need
git rebase -i --root
for deeper edits.
4. Secret: Amend with --reset-author
- If you want to reset the author to your current Git config (e.g., after cloning a repo with someone else's commits):
git commit --amend --reset-author
5. Staging Fixups Without Losing Work
- Suppose you staged changes but realize they belong in the last commit:
git commit --amend --no-edit # Absorbs staged changes
- Pro Tip: Combine with
git add -p
to stage partial changes.
6. Undoing an Amend
- Oops! Amended the wrong commit? Recover it via:
git reflog # Find the pre-amend commit hash
git reset --hard HEAD@{1} # Restore to the old state
When Not to Use --amend
On commits shared with others (unless you coordinate force-pushes).
If you've already pushed to a public branch (use
git revert
instead).
Conclusion
git commit --amend
is a lifesaver for fixing small mistakes before sharing your work. Whether you need to update a commit message, include missing files, or correct authorship, this command helps maintain a clean Git history.
Final Pro Tip
Combine --amend
with git rebase -i
for interactive commit editing:
git rebase -i HEAD~5 # Then mark commits with 'edit'
git commit --amend # Make changes during rebase
git rebase --continue
Up Next in the Series: git filter-branch
– Rewrite Git history (caution: destructive)
Daily advance GIT tips in your inbox---worth starting? Respond to my poll here🚀
For more useful and innovative tips and tricks, Let's connect on Medium