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 --force

Step-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 detail

2. 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 --amend

This 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 --force

5. Important Notes About Force Pushing

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~N

Then mark the commit you want to change with reword or r and save/exit.

Best Practices

  1. Always check your commit messages before pushing
  2. If working on a shared branch, coordinate with your team before force pushing
  3. Keep commit messages clear and descriptive
  4. Use the present tense in commit messages (e.g., “Add feature” not “Added feature”)

Common Mistakes to Avoid

Date
20.05.2025
Categories
  • Git
Tags
  • Git