How to change the commit message in git
Today we’ll show you how to change the commit message in git. This is a small article but very useful for those who work with git.
change commit message after pushed on remote, change the last commit message, change the pushed commit message, change the previous commit message in git, change the multiple commit messages in git, edit the commit message in github, How To Amend Git Commit Message, How can I edit / fix the last commit’s message?, How to modify existing, unpushed commit messages?
Checkout more articles on Git
In this article, we’ll show you both cases where you might need to change the commit message in git history.
To change commit message in git
- Change unpushed commit message
- Change commit message that already pushed
- Change more than one commit messages
1. Change unpushed commit message
Let’s assume that you have committed the code but it’s not pushed yet. In this type of case, you can change the most recent commit message by running the command below.
1 | git commit --amend |
After executing the command, Editor will be auto open where you can change the commit message then save and close it.
You can also change the commit message directly in the command line.
1 | git commit --amend -m "Write your new commit message" |
2. Change commit message that already pushed
If you have already pushed your code to git remote branch then first execute above command and follow the command below to forcefully push the commit message.
1 | git push <remote> <branch> --force |
OR
1 | git push <remote> <branch> -f |
3. Change more than one commit messages
To change more than one commit message, We need to use rebase
command as mentioned below.
1 | git rebase -i HEAD~<commit_count> |
Use the number of commits instead of <commit_count>
in the above command like git rebase -i HEAD~2
. This will launch the editor where you have to replace the word pick
to reword
. Check below code for your understanding.
Before
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | pick 5f9e37b This is first commit message pick 1ef0598 This is second commit message # Rebase 1b87f21..1ef0598 onto 1b87f21 (2 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. # # Note that empty commits are commented out |
After
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | reword 5f9e37b This is first commit message reword 1ef0598 This is second commit message # Rebase 1b87f21..1ef0598 onto 1b87f21 (2 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. # # Note that empty commits are commented out |
Now save and close the editor. At a time another editor will be open where you can update the commit message. The editor will open as many times as you have passed the number in <commit_count>
.
Run the below command to push the commit on remote branch.
1 | git push <remote> <branch> --force |
OR
1 | git push <remote> <branch> -f |
That’s it for today.
Thank you for reading. Happy Coding!