How to Fix a Git Commit Message With git amend
A typo in the most recent commit message is a one-command fix: git commit --amend -m "correct message" rewrites it in place, no push, no history loss. Here's how it works and when it's safe.
What git commit --amend does
--amend replaces the most recent commit with a new one. The new commit has the same
parent, the same file tree, and the same author - but a fresh SHA and (optionally) a
new message or staged changes. To the history it looks like you never made the mistake.
Fix the message before you push
cd /workspace/repo
# See the current (bad) message
git log -1 --oneline
# Rewrite just the message - nothing else changes
git commit --amend -m "feat: add new health endpoint"
# Confirm the new message
git log -1 --oneline
The old commit hash is gone and a new one takes its place. Because the commit hasn't been pushed yet, nobody else has the old hash - so this rewrite is completely safe.
What if you also want to add a forgotten file?
Stage it first, then amend:
git add src/health.py
git commit --amend --no-edit # keep the existing message, add the staged file
--no-edit skips the editor so the message stays unchanged.
When NOT to amend
Once a commit is on a remote shared branch, --amend becomes a force-push - it
rewrites history under your teammates' feet. The rule is simple:
- Unpushed commit - amend freely.
- Pushed to a shared branch - don't amend; use
git revertor make a follow-up commit instead.
If you must amend a pushed branch (e.g. your own feature branch, no open PRs),
follow up with git push --force-with-lease (safer than --force - it checks that
nobody else pushed in the meantime).
Conventional commit format
Keeping to a consistent format makes amended messages easier to get right. The Conventional Commits spec uses:
<type>(<scope>): <short summary>
feat: add new health endpoint
fix: correct off-by-one in pagination
chore: update dependencies
Pair it with commitlint in CI and you catch malformed messages before they ever
reach the remote.
Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.
Try it in a workspace →What you'll practice
- Rewriting a typo'd commit message with git commit --amend -m
- Adding a forgotten file to the last commit with --amend --no-edit
- Understanding when --amend is safe vs when it requires a force-push
FAQ
How do I fix a typo in a git commit message?
Run git commit --amend -m "correct message" immediately after the bad commit. This rewrites the most recent commit in place with a new message. Only do this before pushing - after pushing it requires a force-push that affects other collaborators.
Is git commit --amend safe?
Yes, as long as the commit hasn't been pushed to a shared remote branch. Amending a local-only commit is completely safe - it rewrites just the last commit and no one else has the old SHA. If it's already pushed, force-pushing rewrites shared history and should be avoided on team branches.
How do I amend a commit without changing the message?
Stage your changes and run git commit --amend --no-edit. It adds the staged files to the previous commit and keeps the existing message exactly as-is.
Keep learning
Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.
Open the workspace →