Git is a powerful version control system used by many developers and organizations all over the world. It allows users to make changes and save them as commits, along with adding a message to indicate the changes made in each commit. Each commit in a git repository has a message that is used by developers to understand the changes made in that commit. Sometimes you may want to make changes to a specific message after you have committed it. In this article, we will learn how to change git commit message.
How to Change Git Commit Message
Here are different use cases to change git commit message.
1. Changing most recent commit
If you want to change the message of most recent commit, enter the following command.
$ git commit --amend
The above command will open the most recent commit message in an editor and allow you to make changes. If you want to completely erase the last message and set a new commit message, use -m option.
2. Push Changes
After you make changes, you need to push the commit to your remote repository, so that it can be applied to other developers’ repositories whenever they pull from it.
You can do this using any of the following commands.
$ git push --force-with-lease <repository> <branch> OR $ git push <repository> +<branch> OR $ git push --force <repository> <branch>
Please note, if someone else has also pushed to the same branch, your push may destroy those changes. Therefore it is advisable to use –force-with-lease option so that it aborts in case there are any upstream changes.
Also, if you don’t specify the branch during push command, then your push may affect multiple branches.
3. Pull from Repository
Now when someone pulls from this repository, they will get an error message. So they need to use the following command to pull from repository. But please note, this will overwrite any local changes made to the branch.
$ git fetch origin $ git reset --hard origin/master # Loses local commits
In this article, we have learnt how to change git commit message. As you can see, changing git history can be tricky and give errors. So it is advisable to avoid changing history as much possible. Even if you change history, it is better if you inform others on your team about it, so that they modify their pull commands accordingly.
Also read:
How to Force Git Pull to Overwrite Local Files
How to Delete Git Branch Locally & Remotely
How to Reset or Revert File to Specific Commit in Git
How to Find & Restore Deleted File in Git Repository
How to Set Vim Default Editor for Git
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.