Clean up historyAlways fixable

Change a commit message you already wrote

How do I edit a Git commit message?

Short answer

For the most recent commit, git commit --amend -m "new message" replaces it. For older commits, git rebase -i lets you mark any of them as reword. Both rewrite history, so if the commit is already pushed you'll need git push --force-with-lease and a word to your team.

quick fix
git commit --amend -m "The message you actually meant"

Rewrites the most recent commit's message in place

Does this match your situation?

  • You committed with a typo, the wrong ticket number, or a message like 'asdf'.
  • Your team's commit convention was not followed and CI is rejecting it.
  • You need to fix the message on a commit further back in history.

Step-by-step fix

  1. Fix the most recent commit

    Amend replaces the last commit with a new one carrying your new message. Nothing about the file contents changes, but the commit hash does, because the message is part of what gets hashed.

    step 1
    git commit --amend -m "Fix: clamp player health to zero on death"
    

    Running git commit --amend with no -m opens your editor with the old message pre-filled.

  2. Fix an older commit with interactive rebase

    Start a rebase covering enough commits to include the one you want, then change its action from pick to reword. Git will stop and open an editor for each one you marked.

    step 2
    git rebase -i HEAD~5
    
    # In the editor, change 'pick' to 'reword' on the target line:
    #   pick a1b2c3d Bad message      →   reword a1b2c3d Bad message
    # Save and close; Git reopens an editor for the new message.
    
  3. Fix several messages at once

    You can mark as many lines as you like with reword in a single rebase. Git walks them in order, pausing at each so you can rewrite the message and continue.

    step 3
    git rebase -i HEAD~10
    # mark every line you want to fix as 'reword'
    # after each editor closes, the rebase continues automatically
    
  4. Push the rewritten history

    Because the hashes changed, the remote will reject a normal push. --force-with-lease overwrites your branch but aborts if someone else pushed in the meantime, which plain --force would silently destroy.

    step 4
    git push --force-with-lease
    

    Only do this on branches you own. On a shared branch, coordinate first — everyone else will need to reset to the new history.

Why this works

A commit's hash is computed from its contents, its parent, its author and its message, so changing the message necessarily produces a different commit. Git doesn't edit the old one; it creates a replacement and moves the branch pointer to it. Everything after the rewritten commit also has to be recreated, because each commit names its parent by hash — which is why rewriting a commit ten steps back rebuilds all ten. The originals stay in the reflog, so a botched rebase is recoverable.

How to stop it happening again

  • Write the message in an editor rather than with -m, so you get a subject line and a body.
  • Adopt a convention like Conventional Commits and enforce it with a commit-msg hook.
  • Use git commit --amend freely before pushing — it's the cheapest fix there is.

Commands used in this guide

git commit --amend

Replace the most recent commit instead of stacking another one on top.

git rebase -i

Reorder, squash, split, reword or drop your recent commits.

git push

Upload your commits to the remote repository.

git reflog

The undo history for everything - the single most important recovery command.

Still stuck?

Search the full command reference and every other rescue guide — there are 56 of them, covering everything from detached HEAD to force-push disasters.

Browse all guides →

Frequently asked questions

How do I change the last commit message in Git?

Run git commit --amend -m "your new message". This replaces the most recent commit with an identical one carrying the new message. If it was already pushed, follow up with git push --force-with-lease.

How do I change an older commit message?

Use git rebase -i HEAD~n where n reaches back past the commit, then change that commit's action from pick to reword. Git pauses and opens an editor so you can write the new message, then replays the rest of the history on top.

Is it safe to amend a commit that's already pushed?

It's safe for you but disruptive for others, because the commit hash changes and anyone who pulled the old one now has divergent history. On a personal branch it's fine with --force-with-lease. On a shared branch, agree with your team first or leave the message alone.

Related rescue guides

← All Git Rescue guides