How can I edit / fix the last commit's message?
Made a typo in one of your commit messages? Or forgot to mention an important detail in the message? Correcting a commit message in Git can be very easy - if it's the very last commit you want to edit!
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Amending the Last Commit
To change the last commit, you can simply commit again, using the --amend flag:
$ git commit --amend -m "New and correct message"
Simply put, this overwrites your last commit with a new one. This also means that you're not limited to just editing the commit's message: you could also add another couple of changes you forgot.
$ git add another/changed/file.txt
$ git commit --amend -m "message"
However, keep two important details in mind:
- Amend only works with the most recent commit. If you notice your mistake only after adding another commit, amend won't help you much.
- Amend rewrites the commit history in your repository: the old commit is replaced by a completely new one (a new and different commit object). This makes it very important that you don't amend (= rewrite) commits that you've already published (via
git push
) to a remote repository, such as GitHub! Because in that case, your colleagues might already be working on a branch based on this commit - which you would try to replace using "amend."
Therefore, use "amend" whenever you want to change / edit your very last and unpushed commit.
In case you are using the Tower Git client, amending your last commit is easily possible right from the commit area interface:
Changing Older Commits
If you want to change older commits, Git also has a tool for this use case:
$ git rebase --interactive
The "interactive rebase" editor, however, is quite an advanced tool that unlocks multiple operations: it's very powerful and a tiny bit dangerous. You should definitely understand what you're doing before using this command!
See here if you really need to use it.
If you want easy access to advanced Git tools like "interactive rebase", the Tower Git client can be helpful. For example, you can simply right-click the commit you want to change and select "Edit Commit Message". In the background, an Interactive Rebase session is performed to make this possible:
No matter if you're using "Interactive Rebase" from the Command Line or with our software, the mode of action is the same as with the --amend
flag: you are rewriting history! Therefore, just as with amend, you should not use interactive rebasing on commits you have already pushed!
What to Do if You've Made a Mistake
When you amend a commit, Git creates a new commit object that replaces the previous commit. However, the previous commit is not entirely lost; it is still recorded in Git's reflog, which stands for "reference log."
This acts like a diary, logging all the recent commits and updates made to the tips of branches in your local Git repository. It essentially tracks where the HEAD and branch references have been in the past, enabling you to recover or revisit previous states of your repository when necessary.
If you accidentally amend a commit that you didn't intend to change, or if you need to revert to the previous state of a commit for any reason, you can use the reflog to find and restore the previous commit. The reflog entries have a unique identifier (a SHA-1 hash) that you can use with commands like git reset
or git checkout
to restore a previous state.
Learn More
- Check out the chapter Undoing Things in our free online book
- More frequently asked questions about Git & version control