Lesson 24 +20 XP

Git Amend

git commit - amend lets you fix the last commit instead of making a new one.

Fix the commit message

Forgot to spell something right in your latest message?


bash
git commit - amend -m "Fixed message"

This replaces the last commit's message with the new one.

Add forgotten changes

If you forgot to stage a file before committing:


bash
git add missing-file.txt
git commit - amend

The missing file gets added into the last commit, so it becomes one complete commit instead of two.

Important warning

Amend rewrites history. The old commit is replaced by a new one with a new hash. This is fine for local, unpublished work, but do not amend commits you already pushed, because everyone else has the old version.

TL;DR

  • git commit - amend edits the most recent commit.
  • Use -m to fix the message.
  • Stage files first, then amend to add them.
  • Amending rewrites history, so avoid it on pushed commits.