How to modify the commit messages in Git
In this tutorial, we are going to learn about how to change the commit git messages in local and remote repositories.
Changing Last commit git message locally
-
Inside the command line navigate to the repository you need to modify the commit message.
-
Run
git commit
command followed by the--amend
flag.
git commit --ammend
This command will open an editor with a recently commit message, where we can edit the message once you are done with editing save it.
Note: The above two steps only change the locally commit git message.
Changing last commit message remotely
-
Follow the above two steps
-
Run the
git push
command followed by the--force
flag to change remotely commit message.
git push --force
Changing multiple Commit messages
If you want to change the multiple commit message instead of the last commit message you need to follow the below steps.
-
Inside the command line navigate to the repository you need to modify the commit message.
-
Run
git rebase -i HEAD~n
command to display the last n number of commit messages.
git rebase -i HEAD~4
This above command will display the list of previously committed four messages, which looks similar to the below text.
pick f3999e6 thumbnail size modified
pick 67e92a3 updated size
pick 384ff6f screenshot added
pick 3c63d17 added some text
# Rebase 28d77e0..3c63d17 onto 28d77e0 (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
-
Once you’re done with editing save your commit file.
-
Run the
git push
command followed by the--force
flag to force the push the changed commit messages to a remote git repository.
git push --force