Skip to content

Git - Commit Changes

Sometimes you make a commit and then realize you made a mistake in the commit message, or you forgot to include a file, or you want to add a small change to that last commit. Git’s commit --amend operation allows you to modify your most recent commit.

Important: Amending a commit rewrites that commit. It creates a new commit ID. If you have already pushed the original commit to a shared remote repository, you should generally avoid amending it. If you do amend a pushed commit, you’ll need to force-push your changes (git push --force-with-lease is safer than git push --force), which can disrupt collaborators who have based their work on the original commit. Amending is safest for local commits that haven’t been shared yet.

Let’s say Jerry made a commit. He can check the commit log:

[jerry@dev-machine project]$ git log -1 --oneline
cbe1249 (HEAD -> main) Implemented my_strlen function

Original commit details:

commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277
Author: Jerry Mouse <jerry@example.com>
Date: Wed Sep 11 08:05:26 2013 +0530
Implemented my_strlen function

Now, Jerry realizes he needs to update the return type of my_strlen in string_utils.c and wants to include this change in the same commit, or perhaps just wants to reword the commit message.

First, he makes the necessary code changes. Then, he stages them:

[jerry@dev-machine project]$ # ... Jerry modifies string_utils.c ...
[jerry@dev-machine project]$ git status -s
M string_utils.c
[jerry@dev-machine project]$ git add string_utils.c
[jerry@dev-machine project]$ git status -s
M string_utils.c

Now, instead of creating a new commit, Jerry uses git commit --amend. This will take the currently staged changes and combine them with the contents of the previous commit. It will also open his configured text editor to allow him to change the commit message.

[jerry@dev-machine project]$ git commit --amend

If Jerry only wanted to change the commit message without adding new file changes, he could run git commit --amend without staging new files. To change the message directly from the command line and include staged changes:

[jerry@dev-machine project]$ git commit --amend -m 'Changed return type of my_strlen to size_t and updated logic'
[main d1e19d3] Changed return type of my_strlen to size_t and updated logic
Date: Wed Sep 11 08:05:26 2013 +0530
1 file changed, 5 insertions(+), 2 deletions(-)

Now, if Jerry checks the log, he’ll see a new commit ID for the amended commit, and the old commit cbe1249 is no longer the tip of the main branch:

[jerry@dev-machine project]$ git log -1 --oneline
d1e19d3 (HEAD -> main) Changed return type of my_strlen to size_t and updated logic

The full log for the new commit:

commit d1e19d316224cddc437e3ed34ec3c931ad803958 (HEAD -> main)
Author: Jerry Mouse <jerry@example.com>
Date: Wed Sep 11 08:05:26 2013 +0530
Changed return type of my_strlen to size_t and updated logic

The original commit cbe1249 is now an orphaned commit (unless another branch or tag points to it) and will eventually be garbage collected by Git if it’s unreachable.