Looking back and undoing · lesson 4 of 7
Commands and ideas in this lesson
Agit commit --amendreplace the last commit
B--no-editkeep the message as it was

Fixing the last commit

Last updated

A typo in the message, or a file you forgot to add: the last commit is the easiest one to fix. In this lesson you'll use git commit --amend, see that it really makes a new commit in place of the old one, and learn when not to use it.

By the end of this lesson you can

  • Change the last commit's message with git commit --amend
  • Add a forgotten change to the last commit
  • Explain that amend replaces the commit with a new one
  • Know not to amend a commit you've already shared

Fix a message

git commit --amend -m "new message" replaces the last commit's message. Nothing else about it changes.

git commit --amendCareful: changes files or history, but Git can usually get it back: it changes history, but only the newest commit, and lesson 2.7 shows how to get the old one back.

Ana just committed the halo-halo recipe with a typo in the message. Commit it, then fix the message.

Git sandbox
  1. Make a file halo-halo.md (try echo "# Halo-halo" > halo-halo.md)
  2. Commit it with the message "Add hal-halo recipe" (typo on purpose)
  3. Fix the message to "Add halo-halo recipe" with --amend

Working folder

no changes

Staging area

nothing staged

Repository

4 commits on main

You are in ~/kusina, which has four commits.

Commit graph

  1. 1c17fad HEAD →main Add a README
  2. 9c53888 Add leche flan recipe
  3. 1f359a2 Add sinigang recipe
  4. 601c654 Add adobo recipe

Add what you forgot

Forgot a file? Stage it and amend. --no-edit keeps the message as it was:

git add ube.md
git commit --amend --no-edit

The last commit now holds both files, as if you'd added them together the first time.

Git sandbox
  1. Commit a new halo-halo.md with a good message
  2. Make ube.md, then add it to the same commit with --amend --no-edit

Working folder

no changes

Staging area

nothing staged

Repository

4 commits on main

You are in ~/kusina, which has four commits.

Commit graph

  1. 1c17fad HEAD →main Add a README
  2. 9c53888 Add leche flan recipe
  3. 1f359a2 Add sinigang recipe
  4. 601c654 Add adobo recipe

What amend really does

A commit can never be changed: its hash is made from its contents, so a changed commit would be a different commit. --amend makes a new commit with the new message or contents and the same parent, moves the branch to it, and leaves the old one behind.

$ git add halo-halo.md
$ git commit -m "Add hal-halo recipe"
[main 9ab1d1c] Add hal-halo recipe
 1 file changed, 1 insertion(+)
 create mode 100644 halo-halo.md

Working folder

no changes

Staging area

nothing staged

Repository

5 commits on main

  1. 9ab1d1c HEAD →main Add hal-halo recipe
  2. 1c17fad Add a README
  3. 9c53888 Add leche flan recipe
  4. 1f359a2 Add sinigang recipe
  5. 601c654 Add adobo recipe
1 of 3 Ana commits the halo-halo recipe with a typo in the message.

Spot the trap: amending a shared commit

Amending is fine for commits only you have. Once you've shared a commit (Stage 4: pushed it), someone else may have it and build on it.

Spot the trapAna and Joaquin

Ana pushes “Add pancit recipe” to the shared repository. Joaquin pulls it. Then Ana notices a typo, amends the commit, and pushes again.

What happens if…

Check what you learned

After git commit --amend, the commit’s hash…
You forgot to add ube.md to your last commit, which you haven’t pushed. What’s simplest?
Which commits is it safe to amend?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git