Git Commit Amend

Did you make a spelling mistake in your last commit message? Did you forget to add a file? You do not need a new commit for it. The git commit --amend command replaces the last commit with a new and corrected one.

Change the last commit message

Terminal output

$ git log –oneline
f5ab70f (HEAD -> main) Add frist post
$ git commit –amend -m "Add first post"
[main d3c22eb] Add first post
Date: Sat Jan 10 10:42:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 post1.txt
$ git log –oneline
d3c22eb (HEAD -> main) Add first post

Look at the hash. It is different after the amend, because it is a new commit. The old commit is not in the history anymore.

Add a forgotten file to the last commit

Terminal output

$ echo "Hello again" > post2.txt
$ echo "Hello, world" > post1.txt
$ git add post1.txt post2.txt
$ git commit –amend –no-edit
[main 79349a3] Add first post
Date: Sat Jan 10 10:42:00 2026 +0000
2 files changed, 2 insertions(+)
create mode 100644 post1.txt
create mode 100644 post2.txt
$ git show –stat –oneline
79349a3 (HEAD -> main) Add first post
post1.txt | 1 +
post2.txt | 1 +
2 files changed, 2 insertions(+)

The option --no-edit keeps the old message. Now one commit has both files.

Change only the author or date

git commit --amend --reset-author --no-edit

Important warning

Amend does not edit the commit, it creates a new commit and drops the old one. So never amend a commit which you already pushed and others may have taken. Their history will not match yours. If you must, you need a force push, which we discuss in Git Push.

SituationWhat to do
Commit is only on your computerYou can amend freely
Commit is pushed, but only you use that branchAmend and force push with --force-with-lease
Commit is pushed on a shared branchDo not amend. Make a new commit, or use git revert
Note: If you amended by mistake, the old commit is not lost for some time. You can find it with git reflog. Next: Git Reset.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.