by Sai gowtham

How to modify the commit messages in Git

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

  1. Inside the command line navigate to the repository you need to modify the commit message.
  2. 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

  1. Follow the above two steps
  2. 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.

  1. Inside the command line navigate to the repository you need to modify the commit message.
  2. 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.
  1. Once you’re done with editing save your commit file.
  2. 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

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
26,545 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
221,520 students enrolled
44 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
114,575 students enrolled
21 hours of video content
View Course