How to Amend a Commit with git commit --amend
Short answer
git commit --amend replaces the most recent commit with a new one, allowing you to correct the commit message, add missed files, or remove a file that should not have been included. To change only the message, run git commit --amend -m "Corrected message"; to add a missed file without changing the message, stage it first with git add <file> and then run git commit --amend --no-edit. Because amending rewrites the commit — producing a new commit object with a different hash — any version of the old commit on a remote becomes inconsistent with your local history. If the commit has already been pushed, you must force-push to update the remote: git push --force-with-lease is the safer option because it aborts if someone else has pushed in the meantime. For commits further back in history, use git rebase -i HEAD~N, mark the target commit with edit, make your changes, and then run git commit --amend followed by git rebase --continue.
In a hurry? Watch our brief 1-minute vertical video that summarizes the info below.
Did you ever make a commit only to realize you forgot to add a file? Or maybe you made a typo in the commit message?
Don't worry, you don't have to create a new commit to fix this. You can simply amend the previous one.
A Word of Caution
You should only amend commits that have not been pushed to a remote repository yet. Amending a commit that other people have already based their work on can cause a lot of problems.
Adding a Forgotten File to the Last Commit
Let's say you forgot to add a file to your last commit. To add it, you first need to stage the file:
git add forgotten-file.txt
Then, you can run the git commit --amend command. If you don't want to change the commit message, you can add the --no-edit flag:
git commit --amend --no-edit
This will add the staged file to your last commit, without changing the commit message.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Free. We email you a ZIP with our cheat sheets.
Changing the Last Commit Message
If all you want to do is to edit the message of your last commit, you can run the git commit --amend command without staging any new files:
git commit --amend
This will open your default text editor, allowing you to edit the message of your last commit.
After you've saved and closed the editor, the new commit message will be applied!
No Command Line Needed
Amending Commits in Tower
In the Tower Git client, amending a commit is very easy. You can right-click the commit in the history and choose "Amend [COMMIT HASH]...".
Even better, in the "Working Copy" view, you can hold down the Option key (on Mac) or the Alt key (on Windows) and the "Commit" button will turn into an "Amend" button.
Learn More
- Check out the chapter on Undoing Things – Desktop GUI edition in our free online book