Change a Git commit message
Fix the latest message or reword an older commit, preserve staged work during metadata-only edits, and understand the collaboration cost of rewriting history.
For a message-only change to the latest commit, use:
git commit --amend --only -m "Fix login timeout handling"
--only with --amend and no paths keeps staged changes out of the replacement commit. Without it, amend normally includes the current index, which may add code changes you did not intend to include. Omit -m if you want to edit a multiline message in your configured editor.
Reword an older commit
With local work saved, select the relevant history:
git branch backup-before-reword
git rebase -i HEAD~5
Change pick to reword for the target entry. Git opens an editor for its message and replays later commits. The example requires at least five first-parent steps to exist. git rebase -i --root includes the root commit; ordinary interactive rebase does not preserve merge topology by default.
Resolve any replay conflicts, stage the resolution, and use git rebase --continue, or use git rebase --abort to abandon the operation.
Decide whether published history should change
Even a message is part of the commit object. Changing it changes the ID and can change descendant IDs. The old object may remain reachable through other refs or reflogs; it does not instantly disappear.
Avoid rewriting shared history for a cosmetic correction. If a published feature branch can be rewritten under your team's policy, fetch and inspect its remote tip first, coordinate with anyone using it, and publish with a suitable lease. --force-with-lease checks the expected remote state; it cannot establish that no one has based work on the old commits.
After a rewrite, collaborators may need to rebase or otherwise reconcile their work. A blanket instruction to hard-reset every clone can destroy local work and is not an appropriate recovery plan.
Add a forgotten file without changing the message
git add -- path/to/missing-file
git diff --cached
git commit --amend --no-edit
Here including the index is intentional. Check that it contains only the changes you want to add to that commit. --no-edit preserves the message, not the original commit ID.
Write a useful replacement
Use a concise subject that states the change, often in the imperative: “Handle expired session tokens.” Put the reason, constraints, or migration details in the body. A roughly 50-character subject is a common convention, not a Git limit. If the repository uses Conventional Commits or commitlint, follow its actual rules rather than assuming every project requires feat: or fix: prefixes.
See git-commit and git-rebase.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan