Back to Blog

How to Fix a Git Commit Message (Amend, Rebase, and Force-Push Safely)

|
TL;DR: Most recent commit: git commit --amend. An older one: git rebase -i HEAD~N, change pick to reword on that commit. Already pushed: git push --force-with-lease instead of --force.

Every once in a while I make a mistake in a commit message and need to go back and fix it. The fix looks different depending on whether it's the commit you just made or one further back.

Fix the most recent commit message

If the commit you need to fix is the very last one, you don't need a rebase at all:

bash
git commit --amend

This opens your editor with the previous commit message, ready to edit. Change it, save, and exit; the commit is replaced with the corrected message. This is the simplest fix, and it's specifically for the single most recent commit.

Fix an older commit message

For anything further back, use an interactive rebase. Assuming the commit you need to fix is two commits back, for example, replace the number to match how far back it actually is:

bash
git rebase -i HEAD~2

You'll see a list of commits in your editor, each prefixed with pick. Change pick to reword (or just r) on the line for the commit you want to fix, and leave everything else alone. Save and exit.

Git will then present your editor again, this time with just that commit's message. Update it, save, and exit. That's the commit fixed locally.

If you've already pushed

A local fix doesn't touch what's already on the remote. If you've already pushed the commit you just rewrote, the remote will reject a plain push, since the commit history no longer matches:

bash
git push origin main --force-with-lease

Use --force-with-lease instead of a bare --force. Both overwrite the remote branch with your local history, but --force-with-lease first checks that nobody else has pushed new commits since you last fetched. If they have, it refuses instead of silently overwriting their work. A bare --force has no such check. Be careful either way: rewriting pushed history can cause real problems for anyone else who's already pulled the commits you're rewriting.

Fixing "gpg failed to sign the data"

If you hit this error while rewording a commit:

text
error: gpg failed to sign the data
fatal: failed to write commit object

Git is trying to sign your commits, and your GPG key isn't configured correctly. To bypass it temporarily:

bash
git config --global commit.gpgsign false

That disables signing globally until you turn it back on. If you'd rather fix GPG signing properly, or even retroactively sign old commits, this guide from WebDevStudios walks through it in detail.

See git-scm.com's documentation on rewriting history for more on amend and interactive rebase beyond what's covered here.

Where to go next

Return a Number in Words Using JavaScript and How to Take Full Page Screenshots in Google Chrome are two more quick, practical fixes in the same spirit.

FAQ

How do I fix my most recent commit message?

Run git commit --amend, edit the message in the editor that opens, then save and exit. This only works on the single most recent commit.

How do I fix a commit message from further back?

Run git rebase -i HEAD~N, where N is how many commits back the one you need to fix is. Change pick to reword on that commit's line, save and exit, then update the message when your editor reopens.

Should I use --force or --force-with-lease when pushing a rewritten commit?

Use --force-with-lease. It refuses to overwrite the remote branch if someone else has pushed new commits since you last fetched, which a bare --force won't check for.

Why am I getting "gpg failed to sign the data"?

Git is configured to sign commits, but your GPG key isn't set up correctly. Run git config --global commit.gpgsign false to disable signing temporarily, or fix your GPG configuration to keep signing enabled.

Is it safe to rewrite commit history I've already pushed?

Only if you're confident no one else has built on those commits. Rewriting pushed history changes the commit hashes, so anyone who already pulled the old versions will run into conflicts. Coordinate before doing this on a shared branch.

Share this article:

Related Posts