How to Change Commit Message In Git
- Steps to changing commit message
- Amending the Most Recent Commit
- Amending after Push
- The git add and git commit Commands
- The git push Command
Many programmers underestimate the role of the commit message, while it is very important for managing the work. It helps other developers working on your project understand the changes that you have made. So it must be as concrete, well-structured and clear as possible.
Steps to changing commit message¶
In this snippet, we are going to show you the steps to follow if you want to change your commit message in Git.
Run Git Rebase in Interactive Mode¶
Firstly, you should run the git rebase in interactive mode and provide the parent commit as the argument, like this:
git rebase -i HEAD~1
After the first step, the editor window will show up offering you to input the command for each commit. All you need to do is typing "reword" at the beginning of the first line and save the file:
Enter a new commit message¶
After the second step, you will need to work with the last editor window. Type a new commit message and save the file.
Amending the Most Recent Commit¶
You can use --amend flag with the git commit command to commit again for changing the last commit:
git commit --amend -m "New commit message"
Running this will overwrite your recent commit with a new one. It is important that you don't amend commits that are already pushed to a remote repository. Thus, use the --amend flag whenever you want to change recent and unpushed commit.
It will also allow you to add another changes that you forget to do using the git add command:
git add more/changed/w3docs.txt git commit --amend -m "message"
The -m option allows writing the new message without opening the Editor.
Amending after Push¶
It is not recommended to change a commit that is already pushed because it may cause problems for people who worked on that commit.
If you change the message of the recent pushed commit, you should force push it using the git push command with --force flag:
git commit --amend -m "New commit message." git push --force origin branchName
Here is an alternative and safer way to amend the last commit:
git push --force-with-lease repository-name branch-name
The git add and git commit Commands¶
The git add command is used for adding changes in the working directory to the staging area. It instructs Git to add updates to a certain file in the next commit. But for recording changes the git commit command should also be run. The git add and git commitcommands are the basis of Git workflow and used for recording project versions into the history of the repository. In combination with these two commands, the git status command is also needed to check the state of the working directory and the staging area.
The git push Command¶
The git push command is used to upload the content of the local repository to the remote repository. After making changes in the local repository, you can push to share the modification with other members of the team.
Git refuses your push request if the history of the central repository does not match the local one. In this scenario, you should pull the remote branch and merge it into the local repository then push again. The --force flag matches the remote repository’s branch and the local one cleaning the upstream changes from the very last pull. Use force push when the shared commits are not right and are fixed with git commit --amend or an interactive rebase. The interactive rebase is also a safe way to clean up the commits before sharing. The git commit --amend option updates the previous commit.