git commit --amend: fixing the last commit
Updated July 31, 2026
git commit --amend replaces the last commit with a new one: with a corrected message, with a file you forgot, or with both at once.
The situation is ordinary: you commit, and a minute later you spot a typo in the message or a file you left out. There is no need for a second commit called "fix the previous one".
Put the forgotten file into the index and repeat the commit with --no-edit so the message stays as it was:
$ git add images.txt
$ git commit --amend --no-edit
[main 02990aa] chapter about deadlines
Date: Tue Mar 17 12:04:31 2026 +0300
2 files changed, 2 insertions(+)
create mode 100644 chapter.txt
create mode 100644 images.txtYou fix the message with the same --amend, just with new text:
$ git commit --amend -m "chapter about the delivery deadlines"With neither -m nor --no-edit, git opens an editor holding the old message, which is handy when you want to add a body instead of replacing the whole line.
What actually happens
The old commit is not edited; it cannot be changed at all. Git builds a new snapshot from the current index, attaches the message to it, and moves the branch onto it. The hash comes out different because the content is different:
$ git log --oneline
02990aa chapter about deadlines # was 8f1c4d2 before the amend
a70cfa7 report: first versionThe previous commit stays in the database for a while, but nothing points at it any more. If you ran --amend by mistake, find it in the output of git reflog and go back to that hash.
Why you should not amend what you pushed
As long as the commit lives only on your machine, --amend is safe. A commit already on the server may have been pulled by someone, and the replacement will diverge from their history. The server simply refuses such a push:
$ git push origin main
To github.com:anna/report.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:anna/report.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart.It is right: it holds a commit that no longer exists in your history. You can force it through with git push --force, and on your own branch that nobody has pulled that is tolerable. On a shared branch --force erases history for everyone working on it at once, so the right move there is a plain new commit on top.
Common mistakes
--amend without git add changes only the message: edits on disk will not enter the commit unless you put them into the index. The flag works on the last commit only; for older ones you need git rebase -i. And the rule about "finishing up": if separate work happened between the commit and the edit, make a new commit instead of stretching the old one.
Where this is in the book
The full treatment is in Chapter 4. Inspect, undo, ignore. Next to it, commit message and git reflog are worth a look.
To practise it by hand, take the interactive lesson Undoing changes.