How to Amend a Commit with git commit --amend
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!
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!
Tip
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 in our free online book
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.