Home » Source Code Management ( SCM ) » Git » How to change last commit message and code for local and remote git ?

How to change last commit message and code for local and remote git ?

Sometimes, you commit some changes in git, as well as push those to remote server and later realises that you need to change those messages for some reason. [ Note: Git only allows to change the last commit message, so if you want to change commit message after adding few more commits, then you are out of easy luck 🙂 ]

In this post, we will demo how you can change the “Last commit” message in a git for local git as well as remote git. Git provides command “git commit –amend” which allows you to change the last commit message.

Changing commit message locally

$ git clone https://github.com/lynxbee/git-helloworld.git
 git log
commit f96a4d72ebcbbf99ed3c0b9495c87ab025dcebdf (HEAD -> test_git_amend)
Author: Linux Developer <social@lynxbee.com>
Date:   Thu Jun 20 08:29:12 2019 +0530

    Initial commit

Now, lets add some helloworld.c and commit it and see the “git log”

Here, for now everything is fine and we pushed the code to remote repository. In our case to github at https://github.com/lynxbee/git-helloworld/commits/test_git_amend

Now, lets demo how you can change this last commit “Added helloworld program” to some new message using “git amend” command as,

$ git commit --amend
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon May 11 10:15:52 2020 +0530
#
# On branch test_git_amend
# Changes to be committed:
#       new file:   helloworld.c
#

Once you enter, git commit –amend command, you will see your last commit message opened in edit mode, which you can change for example to some message as below.

Added helloworld program

This is helloworld program written to test git amend, this message is
amended using "git commit --amend" command.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon May 11 10:15:52 2020 +0530
#
# On branch test_git_amend
# Changes to be committed:
#       new file:   helloworld.c

Now when we save this , we can see the logs updated as,

$ git log
commit 5d86871062e30a82f70f100c2027bb2ea6b6d13a (HEAD -> test_git_amend)
Author: Lynxbee Developer <social@lynxbee.com>
Date:   Mon May 11 10:15:52 2020 +0530

    Added helloworld program
    
    This is helloworld program written to test git amend, this message is
    amended using "git commit --amend" command.

commit f96a4d72ebcbbf99ed3c0b9495c87ab025dcebdf
Author: Linux Developer &amp;lt;social@lynxbee.com&amp;gt;
Date:   Thu Jun 20 08:29:12 2019 +0530

    Initial commit

As you can see, we successfully changed the last commit message in local git.

Changing commit message in remote git

As, we have pushed the commit message to remote git, now if we try to push the latest amended code to remote server as,

$ git push origin test_git_amend

We get an error while pushing local amended commit to remote git as,

$ git push origin test_git_amend 
To https://github.com/lynxbee/git-helloworld.git
 ! [rejected]        test_git_amend -&gt; test_git_amend (non-fast-forward)
error: failed to push some refs to 'https://github.com/lynxbee/git-helloworld.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

This is because, we have changed something in local git, which is different that remote git, so git expects we should first merge remote changes to local git and then only push our changes to remote.

But in our case, we have just changed message and we don’t want to create another merge commit, so we will just force push this commit as,

$ git push origin test_git_amend  --force
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 447 bytes | 447.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/lynxbee/git-helloworld.git
 + 312cc27...5d86871 test_git_amend -&gt; test_git_amend (forced update)

This will successfully change remote commit and you can see at https://github.com/lynxbee/git-helloworld/commit/5d86871062e30a82f70f100c2027bb2ea6b6d13a

Changing code from last commit

You can also change the code in files committed in your last commit as,

  • Modify the files you had changed in your last commit.
  • Use “git add filename” command to add your newly changed code
  • Use “git commit –amend” to amend the message.
  • Use “git push origin remote_branch –force” command to change last committed code and message from remote git.

In short the commands are,

  • For changing local git last commit message
    $ git commit –amend
  • For remote git last commit message
    $ git commit –amend
    $ git push origin remote_branch –force
  • For Modifying last commit message and code
    $ modify code
    $ git add filename
    $ git commit –amend


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment