How update last or older git commit message.


Many times we need to update our previous commit message because of some reason. based on which commit it was it can be done in two ways.
If it was the last commit then we use the
git commit --amend
command.For any older commit we use the
git rebase
command. Updating the last commit.
Updating the last commit
it can be done in the following way using the git amend
command.
git commit --amend -m "your new message"
in this case of the latest commit, you can also update your code and add some more files to your last commit to do that use the following commands.
$ git add "your file name"
$ git commit --amend -m "your new message"
Let's see an example
let we have created a file say foo.txt and we have the content of the file as follows.
added first line
added second line
added third line
and every time we added a line we added a commit message same as the content of the new line, I know it doesn't make sense but for now it is ok. then our git log will look like the below.
command :
git log
output :
jitendra@jitendra:~/Documents/git-fun$ git log
commit 8182867a7a90072b87e99083c5356f77cda6b872 (HEAD -> master)
Author: Jitendra Kumar <username@mail.com>
Date: Wed Apr 26 16:47:56 2023 +0530
added thired line
commit 350488a3d91cbf90370aaecf23dd443fc57b17ea
Author: Jitendra Kumar <username@mail.com>
Date: Wed Apr 26 16:47:05 2023 +0530
added second line
commit 1a8bc1282a7f8e729f12cca002db251ad2f86901
Author: Jitendra Kumar <username@mail.com>
Date: Wed Apr 26 16:46:34 2023 +0530
added first line
now look at our latest commit message I have done a spelling mistake and I want to fix that mistake.
- to do that we can use the below command
jitendra@jitendra:~/Documents/git-fun$ git commit --amend -m "added third line"
[master 3aaecdb] added third line
Date: Wed Apr 26 16:47:56 2023 +0530
1 file changed, 1 insertion(+)
And it is done! ✅️
if I also want to add a new file to that commit. then the command will be :
git add myNewFile.txt
git commit --amend -m added third line and myNewFile
example :
jitendra@jitendra:~/Documents/git-fun$ git add myNewFile.txt
jitendra@jitendra:~/Documents/git-fun$ git commit --amend -m "added third line and myNewFile"
[master c04abd7] added third line and myNewFile
Date: Wed Apr 26 16:47:56 2023 +0530
2 files changed, 2 insertions(+)
create mode 100644 myNewFile.txt
Updating older commit(s).
To update older commit we use git rebase
command. Git has an interactive way of doing it. let's say I added some more lines to my file and the corresponding message for each line. After doing that our git log will look like below.
jitendra@jitendra:~/Documents/git-fun$ git log
commit ac54786c8c58ba34d9cc77075cf58191b2364032 (HEAD -> master)
Author: Jitendra Kumar <username@mail.com>
Date: Thu Apr 27 10:21:28 2023 +0530
added sixth line
commit f91c1ef78f346a5ba16466bbaba950f142cb7c8d
Author: Jitendra Kumar <username@mail.com>
Date: Thu Apr 27 10:20:48 2023 +0530
added fifth line
commit 0b42b2e41e6b7f1081905b28e38f9e1e7c7f6a0e
Author: Jitendra Kumar <username@mail.com>
Date: Thu Apr 27 10:20:05 2023 +0530
added line
commit c04abd7cf56015331e48fcd4f59122bfe7b4e2ff
Author: Jitendra Kumar <username@mail.com>
Date: Wed Apr 26 16:47:56 2023 +0530
added third line and myNewFile
commit 350488a3d91cbf90370aaecf23dd443fc57b17ea
Author: Jitendra Kumar <username@mail.com>
Date: Wed Apr 26 16:47:05 2023 +0530
In this case, I want to update the 3rd commit from the last
which is added line
. and to do that we are going to use the following command.
git rebase -i HEAD~N
Where N is the number of the last commit you want to update from
in our case N=3
so our command will be
git rebase -i HEAD~3
It will open your recent N commits in your(git's) default editor, In my case it looks like the below:
pick c637025 added line
pick 53c517b added fifth line
pick d105566 added sixth line
# Rebase c04abd7..d105566 onto c04abd7 (3 commands)
#
.
.
.
here you can select the commit message you want to update and to do that replace pick to reword
like below in my case.
reword c637025 added line
pick 53c517b added fifth line
pick d105566 added sixth line
# Rebase c04abd7..d105566 onto c04abd7 (3 commands)
#
here you can also select multiple commits by replacing pick to reword
Next, save this file and exit,
in the case of nano text editor say ctrl+s
in Linux/windows or cmd+
in Mac to save. and ctrl+x
to exit.
again text editor will be opened with your commits, and finally, here you can update your commit and save the file.0
added fourth line
# Please enter the commit message for your changes. Lines starting
save and exit and it is done ✅️.
Subscribe to my newsletter
Read articles from Jitendra Kumar Sahu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
