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.
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
This can open up a vim editor the place you’ll be able to change the commit’s 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."
We are able to see the commit message altered with out pushing a brand new commit.
Fixing a pushed commit message
Nonetheless, what occurs if we already pushed the mistaken message to GitHub, as an example.
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"
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
And that is it. We now modified the already pushed commit message.
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