Guide
How to change a git commit message
Commit messages are part of a commit’s identity, so changing one creates a new commit. For the latest commit that’s one command. For older ones, git replays the commits after it.
The last commit: --amend
To rewrite the message of the commit you just made:
git commit --amend -m "Add settings page"
Leave out -m to edit the existing message in your editor instead. Check git status first: anything you’ve staged is added to the amended commit too. To change only the message, unstage it first or run git commit --amend --only -m "…".
An older commit: interactive rebase
Start a rebase from the parent of the commit you want to change (here 4c5d6e7):
git rebase -i 4c5d6e7~1
In the plan that opens, change pick to reword on that commit’s line, then save and close:
reword 4c5d6e7 Add setings page pick 1a2b3c4 Add billing page
Git opens the message in your editor. Fix it, save and close, and git replays the commits after it. Every later commit gets a new SHA, even though its content doesn’t change.
If the commit is the very first one in the repository, use git rebase -i --root.
If you already pushed
The remote still has the old commits, so a normal push is rejected. Update it with:
git push --force-with-lease
--force-with-lease refuses if someone else pushed to the branch since you last fetched. On a shared branch, ask first: everyone who pulled the old commits has to reset to the new ones. On main or a release branch, it’s usually better to leave the message as it is.
Changed the wrong one?
Right after an amend, git reset --soft 'HEAD@{1}' moves back to the original commit and leaves anything the amend added staged. Right after a rebase, git reset --hard ORIG_HEAD takes you back (stash any uncommitted work first). For anything older, see how to undo a git rebase.