DEV Community

Alef Ojeda de Oliveira
Alef Ojeda de Oliveira

Posted on

How to change commit message?

How to change commit message?

If you mistakenly wrote the wrong message git commit -m "the wrong message",
there is a command called git rebase -i HEAD~n.
( Thank God !!! )

1º STEP: KNOW WHERE YOUR COMMIT IS LOCATED.


But before changing the commit message, you need to know the position of the commit in chronological order. For that, just run the command git log --oneline, something like this will appear:

93ed9ab message 1 -> last commit.

93ed9ab message 2

93ed9ab message 3

93ed9ab message 4 -> the wrong message !

Now we know the position of the message !

2º STEP: RUN THE CHANGE COMMAND


To update the message of this commit in position 4, we need to run the command as follows: git rebase -i HEAD~4.

Next, a file in your terminal will open a sequence of commits as follows:

pick 93ed9ab message 4 -> the wrong message !

pick 93ed9ab message 3

pick 93ed9ab message 2

pick 93ed9ab message 1

3º STEP: SET THE COMMIT THAT WANTS TO CHANGE THE MESSAGE.


To change the commit message we need to change the word pick by reword in the message that we are going to change, as follows:

! Tip for inserting or editing file in windows git bash:

1- At the terminal:

  • Press the I key and start editing the file.
  • When finished editing or inserting, press the ESC key, and then to save the edition type :wq and press ENTER.

After the change it will look like this:

reword 93ed9ab message 4 -> the wrong message !

pick 93ed9ab message 3

pick 93ed9ab message 2

pick 93ed9ab message 1

Then save the file and close it.

4º STEP: CHANGE COMMIT MESSAGE

Soon after, another file will be opened for editing the commit message, appearing as follows:

message 4

Follow the previous steps to edit and save the file and ready commit message changed. \o/

### 5º AND LAST STEP: SEND CHANGES TO GITHUB FORCING THE git push.


Now we need to commit the changed message to github. Run the following command:

git push --force

Ready change of the completed message and available on the remote server ! . \o/

#### Congratulations !!!
#### GOOD STUDIES !


Author: Alef Ojeda de Oliveira.

github: nemuba.

linkedin: alef-ojeda.

Top comments (2)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I don't know if there is any risk to git push -f?

Collapse
 
nemuba profile image
Alef Ojeda de Oliveira

I still don't know any problems, I used it a few times, but I didn't identify any problems.
Do you know of a possible problem? Thanks for the comment !