While looking back at your commit history, have you ever encountered any typo or a mistaken message that you need to change. In this short blog we will look how to do it. So lets get into it !
To start of just create a dummy git repository for learning. using the following code:

shivam@sj:~/git-tut$ git init
Initialized empty Git repository in /home/shivam/git-tut/.git/

Now lets add a file temp.txt and add an initial commit

shivam@sj:~/git-tut$ touch temp.txt
shivam@sj:~/git-tut$ git add .
shivam@sj:~/git-tut$ git commit -m "initial commit"
[master (root-commit) f9d591e] initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 temp.txt
shivam@sj:~/git-tut$ git log --oneline
f9d591e (HEAD -> master) initial commit

once we have this done just go forth make some changes to the file and have some more number of commits.

shivam@sj:~/git-tut$ git log --oneline
b98c458 (HEAD -> master) commit num 5
0bd1c82 commit nim 4
327c7e6 commit nom 33
8503722 commit num 2
d826ef2 commit num 1
f9d591e initial commit

Now you see I have made 2 mistakes while making the commits and now I want to fix “nom 33” to “num 3” and “nim 4” to “num 4”.
For this we will utilize rebase command in interactive mode. This command can be used to do a lot of other useful things too but lets keep the discussion short and straight and look how do rename an older commit.

Firstly you need to file git log command and identify the commits messages you need to edit.

shivam@sj:~/git-tut$ git log --oneline
b98c458 (HEAD -> master) commit num 5
0bd1c82 commit nim 4327c7e6 commit nom 338503722 commit num 2d826ef2 commit num 1f9d591e initial commit

Now we need to change commits with hash values “0bd1c82 and “327c7e6”. We need to pick one the hash value of the older commit than that we need to change and the fire a command “git rebase -i <hash-value-of-one previous-commit>”.Which in this case will be “git rebase -i 8503722”.

(note: one can always use “git rebase -i <hash-value-of-initial -commit>” but this will unnecessarily list all the girt commits which might be a lot in some cases. this command will simply list all the commit messages after the commit message whose hash is give as argument to it.)

shivam@sj:~/git-tut$ git rebase -i 8503722

Once you fire this command such a window will be prompted to you:

Don’t worry this is just a text editor, by default git use “vi” but aat the time of installation you may have selected a different one as “VS code” or as in my case it is “nano”. If you see there are already some help comments that are there for help.

we need to focus on this text :

pick 327c7e6 commit nom 33
pick 0bd1c82 commit nim 4
pick b98c458 commit num 5

we need to type “ r ” in front of the commits that we need to change as:

r 327c7e6 commit nom 33
r 0bd1c82 commit nim 4
pick b98c458 commit num 5

now once this change is done just save and exit the editor. Now text editor will reopen again .

we just need to rename the text:

commit nom 33

to what ever new message we want to have

commit num 3

Then again a window will open like this:

again change the text “commit nim 4” to “commit num 4” then save and exit.

As we only had to change 2 message no new text editor will re-appear. Now rebase command is complete and we will get this output:

shivam@sj:~/git-tut$ git rebase -i 8503722
[detached HEAD bf50ada] commit num 3
Date: Tue Sep 21 02:08:43 2021 +0530
1 file changed, 47 insertions(+), 1 deletion(-)
[detached HEAD a6a9712] commit num 4
1 file changed, 26 insertions(+)
Successfully rebased and updated refs/heads/master.

Now if we check out git logs, here is what we see:

shivam@sj:~/git-tut$ git log --oneline
99fccce (HEAD -> master) commit num 5
a6a9712 commit num 4
bf50ada commit num 3
8503722 commit num 2
d826ef2 commit num 1
f9d591e initial commit

Now here is how we do it.
Just look at the hash values before and after rebasing this is because we re-wrote the git commit history again. If you have pushed code to some remote repository you might have to forcefully push and rewrite the history for example:

git push -f origin master

where origin is the name of remote and master is the branch being pushed you may change them accordingly.

(Remember: if multiple people are working on a repository, you should definitely notify all the members working on you code as rewriting your commit history may leave unaware people with certain problem.)

A nomad coder who keeps looking at diiferent languages in search of fun.