Amending Commits

git commit --amend rewrites the most recent commit to fix its message or contents.

Syntaxgit commit --amend

git commit --amend replaces the last commit with a new one. Use it to fix a typo in the message or to add a file you forgot.

Common uses

  • Reword the last commit message.
  • Add staged changes to the last commit.

Amending creates a new commit with a new ID, so only amend commits you have not pushed yet.

Example

Example Β· bash
# Fix the last commit message
git commit --amend -m "Add search bar component"

# Forgot a file? Stage it and fold it in silently
git add search.js
git commit --amend --no-edit

When to use it

  • A developer notices a typo in the last commit message immediately after committing and fixes it with git commit --amend before pushing.
  • A developer forgets to stage a one-line fix and amends the previous commit to include it rather than creating a trivial follow-up commit.
  • A developer amends the author on the last commit after realising they were using the wrong git identity for this client repo.

More examples

Fix the last commit message

Rewrites only the message of the most recent commit without changing any of the staged file contents.

Example Β· bash
git commit --amend -m "feat: add stripe checkout integration"

Add a forgotten file to last commit

--no-edit keeps the existing commit message and simply folds the newly staged change into the previous commit.

Example Β· bash
# Stage the missing file
git add src/checkout.css

# Amend without changing the message
git commit --amend --no-edit

Amend commit author information

Rewrites the author field on the latest commit, useful when the wrong git identity was active at commit time.

Example Β· bash
git commit --amend --author="Ada Lovelace <[email protected]>" --no-edit

Discussion

  • Be the first to comment on this lesson.
Amending Commits β€” GitHub | SoundsCode