Tuesday, July 19, 2022
HomeWordPress DevelopmentGit fundamentals: Altering your final commit message

Git fundamentals: Altering your final commit message


You may by accident make a typo whereas writing your commit message.
Or since you shortly tried to unravel a bug, you by accident left the previous commit message and pushed that.

It is a good follow to maintain the message significant, so you may know what you modify through which commit.

I made a mistaken commit message on my GitHub repo to showcase the way it works.

Wrong git commit message

Within the picture above, we see I dedicated some adjustments with the message: “repair: picture identify mistaken message”.

Let’s have a look at how we are able to repair that.



Fixing a non-pushed commit message

If you have not pushed your code but, it is simpler to repair.

You’ll be able to run the next command.

git commit --amend
Enter fullscreen mode

Exit fullscreen mode

This can open up a vim editor the place you’ll be able to change the commit’s message.

Editing wrong commit message

To edit the textual content, press the i key, and to cease, press esc then wq to avoid wasting the file.

Nonetheless, a quicker approach is to make use of the -m property, which can be utilized to amend the commit message.

git commit --amend -m "repair: picture identify appropriate message."
Enter fullscreen mode

Exit fullscreen mode

We are able to see the commit message altered with out pushing a brand new commit.

Altered commit message



Fixing a pushed commit message

Nonetheless, what occurs if we already pushed the mistaken message to GitHub, as an example.

Wrong commit message on GitHub

No worries, we are able to nonetheless repair it with out messing issues up.

If we’re addressing the final commit, we are able to once more run the next command:

git commit --amend -m "repair: picture identify"
Enter fullscreen mode

Exit fullscreen mode

The subsequent step is to push whereas overwriting the earlier commit message.
For that to work, use the next command:

git push --force-with-lease origin your-branch

# in my case:

git push --force-with-lease origin grasp
Enter fullscreen mode

Exit fullscreen mode

And that is it. We now modified the already pushed commit message.

Changes pushed commit message on GitHub



Thanks for studying, and let’s join!

Thanks for studying my weblog. Be happy to subscribe to my e-mail publication and join on Fb or Twitter



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments