How to amend the last commit in Git?
The git amend is usually used to change the last commit message, but it can also be used to change the content of the last commit itself.
The commands below rewrite the history, so you must proceed with care if the commits have already been pushed to a remote repository.
TLDR: The git commit --amend
command combines the content of the tip of the current branch (HEAD
) with the content of the index and creates a new commit with the same parent as HEAD
. It also opens an editor for you to update the commit message.
Setup
Let’s create a new commit.
alina
alina git:( main ) echo "A" > file.txt
alina git:( main ) git add file.txt
alina git:( main ) git commit -m 'file update'
[main 396811f] file update
1 file changed, 1 insertion(+)
create mode 100644 file.txt
alina git:( main ) █
This leads to the following commits graph.
how to change the commit message of the last commit?
The common way to use git commit --amend
is to edit the last commit message.
The command will open an editor where you can change the commit message.
alina
alina git:( main ) git commit --amend
... editor opens ...
alina git:( main ) █
When you save the file with the new message, a new commit is created with the same content as the previous one but with the updated message.
As always, git does not actually change a commit; it creates a new one (e9d13f
) that is a copy of the previous one (396811
) but with a different message.
The reference HEAD
now points to the new commit, and the previous commit 396811
is no longer referenced by any branch and will eventually be cleaned up by git’s garbage collector.
how to change the content of the last commit?
Let’s update the file and add it to the index.
alina
alina git:( main ) echo "B" >> file.txt
alina git:( main ) git add file.txt
alina git:( main ) █
This time, git commit --amend
will not only open an editor to change the commit message but will also update the content of the previous commit.
alina
alina git:( main ) git commit --amend
...editor opens...
alina git:( main ) █
When you save the editor, the last commit is updated with the new content and results in this new commit graph:
Same as before: we now have a new commit 06d0e3
referenced by HEAD
, and the other two will eventually be cleaned up by git’s garbage collector.
How to see the content of a commit?
If you want to check the content of all those commits, the git show
command is your friend.
The command git show [commit]:[file's name]
can be used to see the content of a file in a given commit.
Let’s start with our HEAD commit 06d0e3
and check the content of the file file.txt
:
alina
alina git:( main ) git show 06d0e3:file.txt
A
B
alina git:( main ) █
That’s indeed our most recent change. Let’s check the previous commit e9d13f
:
alina
alina git:( main ) git show e9d13f:file.txt
A
alina git:( main ) █
That’s the previous content of the file. And finally, let’s check the first commit 396811
:
alina
alina git:( main ) git show 396811:file.txt
A
alina git:( main ) █
These commands only show the content of a specific file.
If you want to see the content of the entire commit, you can use the git show [commit]
command:
alina
alina git:( main ) git show 06d0e3
commit 06d0e3f9a285e7023c0a99af903570639694606d (HEAD -> main)
Author: alina <alina@email.com>
Date: Mon Jun 5 07:22:56 2023 -0700
add file.txt and update
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..35d242b
--- /dev/null
+++ b/file.txt
@@ -0,0 +1,2 @@
+A
+B
alina git:( main ) █
This command shows the content of the commit, the commit message, and the diff between the commit and its parent.
The command can also be used to check the commit message as it was in the previous commit:
alina
alina git:( main ) git show e9d13f
commit e9d13f20962bcfaea338863abff3e0c69ce009eb
Author: alina <alina@email.com>
Date: Mon Jun 5 07:22:56 2023 -0700
add file.txt
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..f70f10e
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+A
alina git:( main ) █
In a typical scenario, you can’t always see the commit graph on disk, but you can use the git reflog
command to see the history of the commits you have been working on.
alina
alina git:( main ) git reflog
06d0e3f (HEAD -> main) HEAD@{0}: commit (amend): add file.txt and update
e9d13f2 HEAD@{1}: commit (amend): add file.txt
396811f HEAD@{2}: commit: file update
b6863a4 (origin/main, origin/HEAD) HEAD@{3}: clone: from ssh://remote.mygit.com/gitpowerup/arenas.git
alina git:( main ) █