CommandGit

Change the last commit message

Rewrites the most recent commit's message in place without creating a new commit, as long as it hasn't already been pushed and shared.

Last updated

Commandgit
git commit --amend -m "New message"

How it works

--amend doesn't create a new commit on top of the last one — it replaces the most recent commit entirely, combining whatever is currently staged with a new message. -m supplies that message directly on the command line instead of opening your configured editor prefilled with the old text.

Because the message is part of what git hashes to identify a commit, amending it produces a brand-new commit hash even though the content is otherwise the same — from git's perspective it genuinely is a different commit, not an edited version of the old one.

Watch out for

  • →Never amend a commit that's already been pushed and possibly pulled by someone else — the new hash means your branch and theirs have now diverged; either don't amend shared commits, or coordinate a force-push (git push --force-with-lease) if everyone agrees.
  • →Omit -m to open your editor prefilled with the existing message instead of replacing it outright — useful when you want to tweak wording rather than retype the whole thing.
  • →--amend also folds in whatever is currently staged with git add, not just the message — if unrelated changes happen to be staged, they'll silently become part of the amended commit too.

Part by part

git commit --amend
Replaces the previous commit rather than adding to it. The result is a new commit object with a new hash, not an edit of the old one.
-m "New message"
Supplies the replacement message directly. Without it, git opens your editor with the old message ready to change.

Common mistakes

  • git commit --amend -m "New message" with files already staged

    Amend folds anything currently staged into the commit as well, so a message fix quietly absorbs unrelated work. Check git status before amending, every time.

  • git commit --amend on a pushed commit, then git push --force

    The rewritten commit has a new hash, so anyone who pulled the old one is now divergent. Use --force-with-lease instead, which refuses if the remote moved since you last fetched.

  • git commit --amend --no-edit -m "New message"

    The two flags contradict each other: --no-edit says keep the existing message and -m supplies a new one. git resolves it in a way that surprises people, so pick one.

Questions

Can I amend a commit further back than the last one?

Not with amend, which only ever touches HEAD. Use an interactive rebase and mark the commit as reword — the same rewriting caveats apply, multiplied by how many commits sit on top of it.

Is it safe to amend after pushing?

Only on a branch nobody else uses. Even then push with --force-with-lease rather than --force, so the push fails if someone did pull it rather than silently discarding their work.

How do I amend without changing the message?

git commit --amend --no-edit keeps the message and folds in whatever is staged. It is the usual way to add a forgotten file to the commit you just made.

Does amending lose the original commit?

It becomes unreachable, not deleted. git reflog still lists it, so you can recover the original message or content until garbage collection removes it weeks later.

Related in Commands