Amending the last commit

Beginner · 3 min

Fix a typo in the last commit message or fold in a forgotten change with git commit --amend, and understand why it replaces the commit instead of editing it.

Before thisWriting good commit messagesUndoing uncommitted changes

The Modern Coder · 3 min

Amending the last commit

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by The Modern CoderReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • git commit --amend replaces the most recent commit with a new one, using the current staging area and, optionally, a new message.
  • Use --no-edit to fold staged changes into the last commit and keep its message, or -m to change the message.
  • An amended commit gets a new ID, because a commit's ID depends on its content. The old commit still exists but nothing points at it.
  • Amend only commits you have not shared. Once others have the old commit, replacing it forces them to untangle two versions.
  • Amend only reaches the latest commit. Fixing an older one needs interactive rebase, covered later.

Small mistakes right after committing

Two slips happen constantly. You commit and instantly spot a typo in the message. Or you commit and realise you forgot to stage a file that belongs in the same change. You could make a second commit called “fix typo” or “oops, forgot a file”, but that fills history with noise nobody wants to read.

For the latest commit, Git lets you redo it.

Fixing a message

I committed a README change to tinynotes with a typo:

$ git commit -m "Document the clear comand"
[main c87f884] Document the clear comand
 1 file changed, 2 insertions(+)

Amend it with a new message:

$ git commit --amend -m "Document the clear command"
[main af8144f] Document the clear command
 Date: Mon Jan 12 10:00:00 2026 +0000
 1 file changed, 2 insertions(+)

The ID changed from c87f884 to af8144f. That’s not a quirk. In the storage lesson, a commit’s ID is a hash of its tree, its parent, its author data and its message. Change the message and the hash is different. Run git commit --amend without -m and Git opens your editor with the old message filled in, which is the right choice for a longer message.

Folding in a forgotten change

I then realised the README line should also mention count. I edited it, staged it, and amended without touching the message:

$ git add README.md
$ git commit --amend --no-edit
[main 5c9e9c4] Document the clear command
 Date: Mon Jan 12 10:00:00 2026 +0000
 1 file changed, 2 insertions(+)

--no-edit keeps the message. Whatever is in the staging area gets folded into the commit. The summary says “2 insertions” because it describes the whole amended commit, not only what I just added. The log shows one commit, not two:

$ git log --oneline -3
5c9e9c4 Document the clear command
27adccf Ignore notes.txt and Python cache files
8b1866b Add a count command

Amend replaces, it doesn’t edit

Here is the part worth understanding. Nothing in Git changes after it is written. Amending builds a brand-new commit from the staging area, with the old commit’s parent, and moves the branch to point at it. The old commit is still in the object store:

$ git cat-file -t af8144f
commit
$ git log --oneline --all
5c9e9c4 Document the clear command
27adccf Ignore notes.txt and Python cache files

af8144f is still a valid commit, but git log doesn’t list it because no branch leads to it. Git removes unreferenced objects eventually, and in the meantime the reflog, which a later lesson covers, can still reach it. If you amend and regret it, that is how you get the old one back. A related fact: when I ran an amend that produced identical content with the same dates, the ID came out identical too, because the hash is a pure function of the content.

What amend doesn’t touch

--amend keeps the original author date and updates the committer date. I checked by committing with a January date, then amending without any date override: the log showed author=2026-01-12 committer=2026-11-23. To make the commit look newly authored, add --reset-author, and --date=now to stamp the current time. I ran git commit --amend --no-edit --reset-author --date=now and the log’s author date jumped to the real current date. That’s rarely wanted, so it’s a flag to know about, not to use.

git commit --amend HEAD~1 is not a thing. I tried it and got “error: pathspec ‘HEAD~1’ did not match any file(s) known to git”. Amend always means the tip of the current branch. Changing an older commit takes interactive rebase, in the rewriting group.

The one rule

Don’t amend a commit you’ve already shared. When you push a commit and then amend it, you have two different commits with the same parent and the same intent. Anyone who already pulled the old one has a history that disagrees with yours, and reconciling means a force push and some unpleasantness. I couldn’t test that in this lesson, because it needs a remote and I had none, so take it from how the IDs work: the amended commit has a new ID, and that is exactly the problem. Amend freely while a commit exists only on your machine.

What the video covers

The Modern Coder’s “Git Amend Tutorial: Rewrite Git History” is about three and a half minutes and was published around 2023. I had no transcript. The title says it covers amending, and from the length it is a quick demonstration of the common cases. It is in English. Dan Gitschooldude’s one-minute version is linked as a second view.

Where this fits

You can fix the last commit. Next, a different kind of cleanup: renaming, moving and deleting files without confusing Git. See renaming, moving and deleting files.

Check yourself

  1. 01You committed and then noticed you forgot to stage one file. What do you run?

    git add the file, then git commit --amend --no-edit. The file joins the last commit and the message stays the same.

  2. 02Does amending edit the old commit in place?

    No. Git builds a new commit and moves the branch to it. The old commit keeps its ID and still exists, but no branch points at it.

  3. 03Why is amending a pushed commit a problem?

    Other people may already have the old commit. Your replacement has a different ID, so their history and yours diverge, and the fix means rewriting history on the shared branch.

  4. 04What happens if you run git commit --amend with nothing staged?

    It rewrites the last commit with the same content, and lets you edit the message. Without a message change, Git builds an identical commit.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark