Git Commit --amend
Last updated
git commit --amend replaces the most recent commit with a new one - letting you fix its message or add files you forgot to stage. It doesn't add a second commit; it rewrites the last one. Because that changes the commit's hash, only amend commits you haven't pushed yet.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git commit --amend | Edit the last commit and its message |
git commit --amend -m "new message" | Change the last commit's message inline |
git commit --amend --no-edit | Add staged files, keep the same message |
Common cases
| Goal | Command |
|---|---|
| Fix a typo in the last message | git commit --amend -m "fixed message" |
| Add a forgotten file to the last commit | git add file then git commit --amend --no-edit |
| Amend a commit already pushed (careful) | amend, then git push --force-with-lease |
Git commit --amend FAQ
How do I change my last commit message?
Run
git commit --amend -m "new message" to replace it inline, or git commit --amend to open your editor and rewrite it there. This rewrites the last commit with the new message - don't do it to a commit you've already pushed unless you're prepared to force push.How do I add a forgotten file to the last commit?
Stage the file with
git add <file>, then run git commit --amend --no-edit. The --no-edit flag keeps the existing commit message and just folds the newly staged changes into the previous commit.Does git commit --amend create a new commit?
No - it replaces the last commit rather than adding one. The result looks like a single commit, but it's technically a new commit with a new hash that takes the old one's place. That's why amending shared history requires a force push.
Is it safe to amend a pushed commit?
Only with care. Amending rewrites the commit, so after pushing you'd need
git push --force-with-lease, which can disrupt anyone who already pulled the original. On a shared branch, prefer a new commit or git revert instead of amending.Can I practice this online?
Yes. Open the terminal playground to run
git commit --amend in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers fixing commits step by step.