How do you amend the last commit message in Git?

Fix a typo or update the last commit message with git commit --amend before pushing.

GitVersion Control

Overview

`git commit --amend` lets you rewrite the most recent commit either just the message, or the message plus the staged changes. It replaces the old commit with a new one.

Key Points

  • •`git commit --amend` opens your editor to change the message; save and close to apply.
  • •`git commit --amend -m "new message"` skips the editor.
  • •You can also add forgotten files: stage them first, then run `--amend`.
  • •Only amend commits that have NOT been pushed amending rewrites history.
  • •If you must amend a pushed commit, you will need `git push --force-with-lease`.

Examples

Change the last commit message (opens editor)

bash
git commit --amend

Change the message inline without opening an editor

bash
git commit --amend -m "fix: correct typo in login validation"

Add a forgotten file to the last commit

bash
git add forgotten-file.js
git commit --amend --no-edit  # keeps the existing message unchanged

Push the amended commit to a remote branch

bash
git push --force-with-lease origin feature/my-branch

--force-with-lease is safer than --force: it refuses to push if someone else pushed in the meantime.

Pro Tip

Use `--no-edit` when you only want to add files to the commit without touching the message. It is faster and avoids accidental message changes.