How to change the last commit message in git?

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 --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"
  2. 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


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

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

    As mentioned previously we can solve this as follows:

    Using git log we 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!