Clear commit messages make project history easier to review, search, and maintain. If you notice a typo, omitted ticket number, or vague description, Git lets you change a commit message. The right method depends on whether the commit is the latest one or an older commit, and whether anyone else already depends on that history.
Before changing a message
Inspect the current branch, working tree, staged changes, and recent history:
git status
git diff --staged
git log --oneline --decorate -5
Amending a commit replaces it with a new commit. Interactive rebase rewrites the selected commit and its descendants. Both can change commit IDs, so avoid rewriting commits that have already been shared unless the team has explicitly coordinated the change.
Edit the latest commit message
If the latest commit is local and has not been shared, amend its message:
git commit --amend -m "Fix login validation for expired sessions"
You can also run git commit --amend without -m to open your configured editor. Review and save the updated message to finish.
Check the index first: amend includes staged changes as well as the existing commit. If you intend to change only the message, make sure there are no unrelated staged files before amending.
Reword an older commit
Use interactive rebase when the commit you want to rename is older than HEAD. Choose a range that includes the target commit. For example, to review the last four commits:
git rebase -i HEAD~4
Git opens a todo list ordered from oldest to newest. Change pick to reword on the commit whose message you want to edit, then save and close the editor:
reword abc1234 Add customer API
pick def5678 Add API tests
pick 987abcd Update documentation
Git prompts for the revised message and then continues replaying the later commits. The rewritten commit and its descendants receive new IDs even if their file changes are otherwise unchanged. If conflicts occur while the commits are replayed, resolve them and run git rebase --continue, or use git rebase --abort to return to the original branch state.
Change several messages in one rebase
In the interactive todo list, change pick to reword for each message you want to edit and leave other commits as pick. Git will prompt for each reworded message in sequence. Review the range carefully so you do not rewrite more history than necessary.
Other interactive-rebase actions include edit to pause for changes to a commit, squash to combine a commit with the previous one while retaining message editing, fixup to combine changes and discard the later message, and drop to remove a commit. These actions change more than the message, so use them only when that is intended.
Verify the result
After amending or rebasing, inspect the history and working tree:
git log --oneline --decorate -5
git status
Confirm the message is correct, the expected commits remain, and no unrelated files were included. If possible, run the project's tests before sharing the rewritten branch.
What if the commit has already been pushed?
Prefer not to rewrite shared history. Other contributors may have based work on the original commit IDs; changing them can require teammates to repair or rebase their branches. If only the wording needs correction, adding a new explanatory commit may be less disruptive.
If the branch is personal, or the team has explicitly agreed to rewrite it, coordinate first and use --force-with-lease rather than plain --force when updating the remote:
git push --force-with-lease origin feature/my-branch
--force-with-lease helps prevent overwriting remote updates you have not incorporated, but it does not make rewriting a shared branch risk-free. Fetch and review the remote state, confirm the branch name, and follow your team's policy.
Write better messages from the start
- Describe the change clearly, such as
Fix expired-session validation on login, rather thanFixorUpdate. - Include an issue or ticket identifier when your project requires one.
- Use the team's convention, such as prefixes for
feat,fix,docs, ortest. - Review the staged diff and message before committing.
- Use
git commit --amendfor the latest unpublished commit and interactive rebase for older unpublished messages. - Coordinate with collaborators before changing any history they may have fetched.
Quick reference: use git commit --amend for the latest commit and git rebase -i HEAD~N with reword for an older one. Rewriting changes commit IDs, so keep it local when possible and coordinate before updating shared history.