Fixing Last Commit In Git Using Amend Flag

There are times we make mistakes as part of our commit and wish to correct them before actually pushing it to the central repository. When you push a wrong change to the central repository, the chances are other developers will fetch the changes to their local copy and later manual interventions are needed to fix the issue.

Git being a distributed version control system comes with a set of features to fix your mistakes and push a clean copy to the central repository. There are commands like git reset, git restore, git revert, and git rebase can be used to fix your commits.

In this article, we will go through the following scenarios.

  • Fixing the last commit message
  • Adding/Appending files to the last commit

NOTE: The commands in the upcoming sections will alter your commit tree. It is not recommended to alter the commits if the changes are already pushed to the central repository.

Create Sample Repository

I will create a sample repository for demonstration purposes. If you wish to follow along you can run the following commands and replicate the sample repository.

  1. Create an empty directory and initialize it as a git repository.
$ mkdir /tmp/git-last-commit
$ cd /tmp/git-last-commit
$ git init .
Initialized empty Git repository in /tmp/git-last-commit/.git/
  1. Create 2 sample commits.
# First commit
$ echo "## sample git commit file - 1 ##" >> sample-git-file.txt
$ git add .
$ git commit -m "sample-git-file.txt added"
[main (root-commit) 647f016] sample-git-file.txt added
 1 file changed, 1 insertion(+)
 create mode 100644 sample-git-file.txt

# Second commit
$ echo "## sample git commit file - 2 ##" >> sample-git-file.txt
$ git add .
$ git commit -m "sample-git-file.txt added line"
[main ea6c1a6] sample-git-file.txt added line
 1 file changed, 1 insertion(+)

The repository now contains two commits and the HEAD is pointing to the latest commit. You can use the git log command to check the commit history.

$ git log
commit ea6c1a6b7be11bb162122362188bbe08a543811e (HEAD -> main)
Author: nixzie-admin <[email protected]>
Date:   Sat Nov 25 22:26:39 2023 +0530

    sample-git-file.txt added line

commit 647f01680107fd3246bb8ee8ab810a927b0dec9f
Author: nixzie-admin <[email protected]>
Date:   Sat Nov 25 22:26:06 2023 +0530

    sample-git-file.txt added

How To Modify Last Commit Message In Git

Let’s assume you made a mistake when writing the last commit message. You can use the git commit command along with the --amend flag to rewrite the last commit message.

$ git commit --amend -m "Added new line to sample-git-file.txt"
[main 0768a7d] Added new line to sample-git-file.txt
 Date: Sat Nov 25 22:26:39 2023 +0530
 1 file changed, 1 insertion(+)

Now when you run the git log command you will see two commits and the commit message of the recent commit is modified too.

$ git log
commit 0768a7d36c8f556673356f1c03ae8572793870d2 (HEAD -> main)
Author: nixzie-admin <[email protected]>
Date:   Sat Nov 25 22:26:39 2023 +0530

    Added new line to sample-git-file.txt

commit 647f01680107fd3246bb8ee8ab810a927b0dec9f
Author: nixzie-admin <[email protected]>
Date:   Sat Nov 25 22:26:06 2023 +0530

    sample-git-file.txt added

NOTE: The --amend flag will not write the changes to the existing commit instead it will create a new commit.

How To Add Files To The Recent Commit In Git

The --amend flag has two behaviours.

  1. It will rewrite the commit message as shown in the previous section.
  2. It will pick any files that are staged and commit them.

Let me create a new file and add it to the last commit along with a new commit message.

$ echo "## second file for testing ##" >> sample-git-file-1.txt
$ git add .
$ git commit --amend -m "1. New line added to sample-git-file.txt
2. sample-git-file1.txt added"

[main 7ed7559] 1. New line added to sample-git-file.txt 2. sample-git-file1.txt added
 Date: Sat Nov 25 22:26:39 2023 +0530
 2 files changed, 2 insertions(+)
 create mode 100644 sample-git-file-1.txt

As you can see from the output of the git log command, my repository has two commits and a new commit was created overwriting the previous HEAD commit.

$ git log
commit 7ed7559106e37ef2b3a561e97efde36831ecfdb7 (HEAD -> main)
Author: nixzie-admin <[email protected]>
Date:   Sat Nov 25 22:26:39 2023 +0530

    1. New line added to sample-git-file.txt
    2. sample-git-file1.txt added

commit 647f01680107fd3246bb8ee8ab810a927b0dec9f
Author: nixzie-admin <[email protected]>
Date:   Sat Nov 25 22:26:06 2023 +0530

    sample-git-file.txt added

You can also use the git show command to check the list of commit messages along with the list of files changed.

$ git show --stat --oneline HEAD
7ed7559 (HEAD -> main) 1. New line added to sample-git-file.txt 2. sample-git-file1.txt added
 sample-git-file-1.txt | 1 +
 sample-git-file.txt   | 1 +
 2 files changed, 2 insertions(+)

Pushing Changes To Central Repository

I have pushed my local changes to github. Let me make one more change in my local repo to the last commit. Here I will just modify the commit message alone which will create a new commit and modify the commit tree.

$ git commit --amend -m "1) New line added to sample-git-file.txt 2) sample-git-file1.txt added"
[main eea76b3] 1) New line added to sample-git-file.txt 2) sample-git-file1.txt added
 Date: Sat Nov 25 22:26:39 2023 +0530
 2 files changed, 2 insertions(+)
 create mode 100644 sample-git-file-1.txt

When I try to push the changes to github it will fail with the following message.

To github.com:nixzie/git-last-commit.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:nixzie/git-last-commit.git'

You can force push the changes to github by using the –force flag. This will overwrite the local changes in github.

$ git push --force

Summary Of Commands Used

This section contains the list of commands used in this guide for your reference.

CommandUsage
git initInitialize an empty repository
git addAdd the file to staging
git commit -m “message”Create a commit
git logView the git commit log
git commit --amend -m “commit message”Force push the local changes to the central repository(github)
git show --stat --oneline HEADShow the changes to the HEAD commit
git push --forceForce push the local changes to central repository(github)

Wrap Up

If the local changes are not pushed to the central repository, you can use the git commit –amend to fix the last commit. But if the changes are already pushed to the central repository it is advisable to create a new commit and push it to the central repository. This way you are not breaking the commit tree and will not create any problems for other users.

Leave a Reply

five × 4 =