Quick cheat sheet to change commit message in git
You just did a commit then realized the message is not the intended one, or you want to git change commit message on multiple commits. Here is what you should do.
To change the last commit, use the --amend
flag this way
git commit --amend -m "<message>"
OR
git commit --amend -c <commit hash>
You can also git change commit messages on the HEAD using a soft reset.
git reset --soft HEAD~1
git commit -m "<message>"
OR
Use the longer route by doing a soft reset followed by a mixed reset. Then commit the changes with a new message.
git reset --soft HEAD~1 git reset HEAD git add <file> git commit -m "<message>"
To edit multiple commit messages or specific commit message that is NOT the HEAD, interactively rebase the commit using any of these commands:
Specify the commit hash for one change.
git rebase -i <commit hash>
Your default text editor opens up. You can then reword
the message.
OR
Edit multiple commit messages by selecting a bunch of commits from the HEAD to the target commit hash.
git rebase -i HEAD~N
where N
is the number of commits, from the top excluding the parent commit hash, whose message you want to edit.
As shown in the next sections of this tutorial, it would help to find a detailed explanation of the git commit message and practice git change commit message using relatable examples.
What is a git commit message? Do's and Don'ts
A git commit message explains why a change occurred in a repository. It plays a significant role in marking software releases and bug fixes.
You will mostly apply git commit messages when collaborating on a project, enabling your teammates to figure out why you made a change on the branch you are working on. Editing commit messages lets you put more descriptive information or entirely overwrite the message.
Despite the excellent side of the git change commit message, you should use it cautiously. For instance, the change could be unconducive for pushed files because changing a commit message alters the SHA id.
Another reason to avoid git change commit message is that the operation automatically commits staged changes even if you had not decided to commit them.
Now that you know what can lead to editing a commit message and the drawbacks of git change commit message, let us see them in action.
Lab setup to practice git change commit message
Create a new repo
I am creating a remote repo called change_commit_message
on GitHub.
After creating the repo, copy its URL and clone it on your preferred terminal or command line.
Configure the text editor
Next, we will configure our default text editor to ease git change commit message. If you have installed a friendlier text editor other than git's default one, vim
, let us configure it as follows:
git config --global core.editor "[your preferred editor] -w"
I am changing my git's default text editor to Visual Studio Code.
git config --global core.editor "code -w"
where the -w
flag informs git to wait for us to change the commit message before recording the input.
Build a commit history
Let's cd
into the new repo and create a few commits in readiness for git change commit message examples.
cd change_commit_message
Second commit
touch file2 git stage file2 git commit -m "Add second commit"
Third commit
touch file3 git stage file3 git commit -m "Add the third commit"
Fourth commit
touch file4 git stage file4 git commit -m "Add fourth commit"
git log
We have four commits.
We have some commits to play with. Let's git change commit messages using relatable examples.
Scenario-1: Editing the last commit message
Git change commit message is quite simple to do on the commit HEAD using either the --amend
flag or git reset soft.
Example-1: Using the amend flag
Assume we want to introduce the article in the fourth commit message. We can achieve that by applying the --amend
flag as follows:
git commit --amend -m "Add the fourth commit"
Let's recheck the history.
git log
The commit message got edited!
Example-2: Doing a soft reset
The reset command for doing git change commit message is
git reset --soft HEAD~1
By writing ~1
, we are telling git to edit one commit from the HEAD. Alternatively, we can replace ~1
with the caret symbol ^
to refer to the last commit, as shown below.
git reset --soft HEAD^
A soft reset undoes the commit HEAD changes. We can then rewrite the message.
git commit -m "Edit the fourth commit"
Our last commit message changed from Add the fourth commit to Edit the fourth commit.
We can also go the longer route by unstaging the file.
Do a soft reset.
git reset --soft HEAD~1
Unstage the last indexed file.
git reset HEAD
Then restage and recommit it using a new commit message
git add . git commit -m "Change the fourth commit message entirely"
Recheck the history.
git log
The last commit message changed from Edit the fourth commit to Change the fourth commit message entirely.
Scenario-2: Git change commit messages on specific file(s)
Want to edit a specific commit message other than the HEAD's? Combine the --amend
and -c
flags or rebase the commit hash interactively.
Example-3: Combine the --amend
and -c
flags
Running the command
git commit --amend -c fc87791e0565c40d5fbdb38fead250e646fdddcd
asks for a commit message to change the commit before the specified hash by opening our text editor.
Let's edit the commit message to Edit the third commit. That changes the commit message before the commit hash fc87791e0565c40d5fbdb38fead250e646fdddcd
Example-4: Interactively rebase a commit hash
Interactive rebase is one of the most familiar ways to git change commit messages. We can use it to edit one commit by rebasing our third last commit as follows
git rebase -i 22e2c5c
The text editor opens up. Change the last commit's command from pick
to reword
, then close the text editor.
The text editor reopens, asking us for a new commit message for the target commit hash. Let's edit the message from Edit the third commit to Change to fourth commit, close the text editor then check the history.
git log
We can also use git rebase -i <commit hash>
to edit multiple commit messages. Better yet, specify N
during the interactive rebase if you know the number of commits from the head to your target base commit hash, as illustrated below.
Scenario-3: Changing multiple commit messages
Example-5: Interactively rebase the HEAD
Replacing N
with 3 in the command
git rebase -i HEAD~N
git rebase -i HEAD~3
opens our default text editor. This time around, let's reword
every commit message, close the editor and see what happens.
The editor reopens three times, asking for respective commit messages. I am replacing each Add or Edit with Track then logging the history to check the result of git change commit messages.
git log
And that's the fundamentals of git change commit message using the rebase commit.
Conclusion
You just learned how to git change commit messages using the --amend
flag, git reset command, and interactive rebase. Now is the time to enjoy your software tracking missions by practicing the commands further and applying them in various projects.
Thank you for the clear details of how to do Example-4, in particular.
You might add the final step, the `git push -f`, for completeness sake.