DEV Community

Gassai Hamza
Gassai Hamza

Posted on • Originally published at potatoesblog.tech on

How to change the last commit message in git?

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 commit --amend , so let's take a look at it.

  1. Using commit --amend
 # 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"
Enter fullscreen mode Exit fullscreen mode
  1. Using commit --amend --no-edit

Sometimes you just want to add a file without changing the commit message, you can then use --no-edit flag:

 git add name_of_the_file # or git add .
 git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

Examples:

Let's create two files: hello.txt and goodbye.txt, add one of them to the staging area and make a commit:

Making a git commit

You notice that we committed without adding goodbye.txt file, and this is the last commit:

Running git log command

As mentioned previously we can solve this as follows:

Making a git commit with --amend flag

Using git log command we check our commit messages:

Running git log command

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!

Top comments (1)

Collapse
 
balizi profile image
Mohamed

great solution