Mistakes in commit messages—typos, vague descriptions, or incorrect references—are common. Fortunately, Git offers simple ways to edit commit messages, whether it’s your most recent commit or a series of past ones.
In this guide, we’ll walk through how to change a Git commit message using safe and effective methods.
🛠 Change the Most Recent Commit Message
If you just made a commit and want to fix the message, you can use:
git commit --amend
✅ Steps:
- Run:
git commit --amend
- Your default text editor (like Vim or VS Code) will open the current commit message.
- Edit the message, save, and close the editor.
⚠️ Note: If you’ve already pushed the commit to a remote repository (e.g., GitHub), you’ll need to force-push the update:
git push --force
Be careful when doing this on shared branches to avoid overwriting others’ work.
🔄 Change an Older Commit Message
If the commit is not the most recent one, use interactive rebase:
git rebase -i HEAD~n
Replace n
with the number of commits you want to look back.
Example:
To change the message 3 commits ago:
git rebase -i HEAD~3
✅ Steps:
- A list of recent commits will appear in your editor:
pick 1234abc Commit message 1 pick 5678def Commit message 2 pick 9abc012 Commit message 3
- Change
pick
toreword
for the commit you want to edit:pick 1234abc Commit message 1 reword 5678def Commit message 2 pick 9abc012 Commit message 3
- Save and close the editor.
- You’ll be prompted to enter a new commit message. Make your changes, save, and close.
- If successful, Git will reapply the commits with the updated message.
⚠️ After rebasing, use
git push --force
if the commits have already been pushed.
📋 Summary
Task | Command |
---|---|
Change latest commit message | git commit --amend |
Change older commit message | git rebase -i HEAD~n |
Push changes to remote safely | git push --force (with caution) |
🧠 Pro Tips
- Use meaningful, concise commit messages that describe why a change was made.
- Avoid amending or rebasing commits on shared branches unless you’re confident everyone else is synced or has agreed to it.
- Tools like GitLens (in VS Code) can help visualize your commit history while editing.
🏁 Conclusion
Changing a commit message in Git is a simple yet powerful way to improve your project’s clarity and history. Whether you’re fixing a typo or clarifying a change, these Git commands give you full control over your commits.