How to change the last commit message in git?
2 min read
It happens many times that you just commit a change and you realized that you made a typo or you forgot to add a file before committing. When this happens you probably think of adding a new commit message, at least this is what I was doing, Thankfully Git has a better way to fix this using
--amend , so let's take a look at it.
# You may want to add some files first git add name_of_the_file # Now you can modify the commit message git commit --amend -m "Your new commit message"
commit --amend --no-edit
Sometimes you just want to add a
filewithout changing the commit message, you can then use
git add name_of_the_file # or git add . git commit --amend --no-edit
Let's create two files:
goodbye.txt, add one of them to the staging area and make a commit:
You notice that we committed without adding
goodbye.txtfile, and this is the last commit:
As mentioned previously we can solve this as follows:
git logwe check out commit messages:
One more thing I should mention is that changing the commit messages could not be a good choice if you already pushed your changes to a
remote repository especially if you are working with a team!