Commits form the core of version control using Git. A commit is a snapshot of our repository. Commits store different versions of our project and are used to commit changes.
Each commit has a message associated with it. This message explains what the commit is about and the files and changes included. We may sometimes need to change the commit message. Let's learn how to modify the commit message for unpushed commits.
Editing the most recent commit message is pretty straightforward. The git commit command with the --amend option provides this functionality.
git commit --amend
The above command will open the configured text editor. We can modify the commit message using this editor.
Use the -m flag to directly give the new message with the Git Commit command.
git commit --amend -m "New Message"
For example, consider a repository with three commits. Run the git log command to view the commit history and commit messages.
git log --oneline
Output:
5f40e3b (HEAD -> master) Commit C
03ec705 Second Commit - Commit B
a338ffe Initial Commit - Added File A.txt
Let's modify the message for commit C.
git commit --amend -m "New Message for Commit C"
Finally, let's view the commit history using the Git Log command.
git log --oneline
Output:
a4e6c34 (HEAD -> master) New Message for Commit C
03ec705 Second Commit - Commit B
a338ffe Initial Commit - Added File A.txt
We can see in the output above that the message for commit C was modified. Also, note that the hash for the commit has changed. This indicates that a new commit was created. The older commit was not altered but deleted and replaced with a new commit. This new commit includes the same changes but a different commit message.
The Git Commit --amend command can only modify the most recent commit message. If we wish to alter some other commit message, we can use the git rebase command. The git rebase command rewrites the history of our project. It works in two modes - standard and interactive. We will use the interactive Git rebase command to alter previous commit messages. Follow the steps below to understand how to use Git Rebase.
Run the Git Rebase command with the -i flag(interactive mode). Pass the hash of the parent of the flawed commit. Or we can use HEAD~N notation to go back "N" commits. Or we can use the caret symbol(^) and the hash of the flawed commit.
git rebase -i <parent-commit-hash>
git rebase -i HEAD~n
git rebase -i <flawed-commit-hash>^
The above command will open a text editor. The editor will contain an entry for each commit. If parent hash is used then all commits after the parent hash is included. If Head~N is used, the last N commits are included. If caret(^) is used, all commits including and after that commit are included.
Next, in the editor, replace pick with reword for each commit whose message you want to alter.
Save and close the editor.
After closing the editor, Git will open a new editor for each commit that you want to reword. Enter the new commit message in the editor.
Save and close the editor. The message will be modified for the desired commits
For example, consider a repository with four commits(A, B, C, D).
git log --oneline
Output:
Output ed2c1ca (HEAD -> master) Commit D
de05d8c Commit C
40d8527 Commit B
1244bf6 Commit A
We want to alter the message for commit B. So, we need to pass the hash of commit B's parent, that is, commit A. Or, we can use HEAD~3 to go back to 3 commits. We can also use the hash of commit B with the caret symbol(^).
git rebase -i 1244bf6 # Using commit A hash
git rebase -i HEAD~3 # Or use HEAD~3
git rebase -i 40d8527^ # Or use commit B hash with ^
The above command will open the editor with the following content.
pick 40d8527 Commit B
pick de05d8c Commit C
pick ed2c1ca Commit D
# Rebase 1244bf6..ed2c1ca onto 1244bf6 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
Replace pick with reword for commit B. Save and close the editor. If we want to alter more than one commit message, then replace pick with reword for other commits as well.
reword 40d8527 Commit B
pick de05d8c Commit C
pick ed2c1ca Commit D
Now, a new editor will open with the following content.
Commit B
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Dec 27 21:34:36 2021 +0530
#
# interactive rebase in progress; onto 1244bf6
# Last command done (1 command done):
# reword 40d8527 Commit B
# Next commands to do (2 remaining commands):
# pick de05d8c Commit C
# pick ed2c1ca Commit D
# You are currently editing a commit while rebasing branch 'master' on '1244bf6'.
#
# Changes to be committed:
# new file: B.txt
Let's modify the commit message and close the editor.
New Message for Commit B using Interactive Rebasing.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Dec 27 21:34:36 2021 +0530
#
# interactive rebase in progress; onto 1244bf6
# Last command done (1 command done):
# reword 40d8527 Commit B
# Next commands to do (2 remaining commands):
# pick de05d8c Commit C
# pick ed2c1ca Commit D
# You are currently editing a commit while rebasing branch 'master' on '1244bf6'.
#
# Changes to be committed:
# new file: B.txt
Finally, let's run the Git Log command to view the commit messages.
git log --oneline
Output:
b02b514 (HEAD -> master) Commit D
4a9b13e Commit C
dbce9f2 New Message for Commit B using Interactive Rebase
1244bf6 Commit A
Observe that the hash for commits B, C, and D have also changed. This is because Git Rebase creates a new commit with the same changes but a different commit message. It does not alter the existing commit. Also, note that the hash for commit A remains the same because commit A was included in the rebasing operation.
Modifying the commit message for the most recent commit is very simple. We can use the Git Commit command with the --amend option. We can also use the -m flag to give the new message with the command.
It is a bit complicated to alter the message of some other commit. We use the interactive Git Rebase for altering previous commits. Git Rebase is used for rewriting the history of our repository. A thing to note is that rebasing will not change the existing commit. Instead, it creates a new commit with the same changes but a new message. We can verify it by observing the commit hash. We can also use interactive Git Rebasing to alter multiple commit messages at once.