How to Change the Last Git Commit Message
Quick Reference
# Change the last commit message
git commit --amend -m "Your new commit message"
# If you've already pushed the commit and need to update the remote
git push --forceStep-by-Step Guide
1. Check the Current Commit History
Before making changes, verify the current commit history:
git log --oneline -3 # Shows last 3 commits
# or
git log -1 # Shows the most recent commit in detail2. Change the Most Recent Commit Message
To modify the most recent commit message:
git commit --amend -m "Your new commit message"3. If You Need to Edit the Message in an Editor
If you prefer to write a longer message or make more complex edits:
git commit --amendThis will open your default text editor where you can modify the commit message.
4. If You’ve Already Pushed the Commit
If you’ve already pushed the commit to a remote repository:
# First, amend the commit locally
git commit --amend -m "Your new commit message"
# Then force push to update the remote
git push --force5. Important Notes About Force Pushing
- Use with caution when force pushing, especially in shared repositories
- It rewrites history, which can cause issues for other collaborators
- If working on a shared branch, consider using
--force-with-leaseinstead:git push --force-with-lease
6. If You Need to Change an Older Commit
For commits that aren’t the most recent, you’ll need to use an interactive rebase:
# Replace N with how many commits back you want to go
git rebase -i HEAD~NThen mark the commit you want to change with reword or r and save/exit.
Best Practices
- Always check your commit messages before pushing
- If working on a shared branch, coordinate with your team before force pushing
- Keep commit messages clear and descriptive
- Use the present tense in commit messages (e.g., “Add feature” not “Added feature”)
Common Mistakes to Avoid
- Forgetting to force push after amending a pushed commit
- Force pushing to shared branches without warning teammates
- Writing unclear or non-descriptive commit messages
- Amending commits that have already been shared with others without coordination
Date
20.05.2025
Categories
- Git
Tags
- Git