How to Change a Commit Message in Git (Last, Old or Pushed Commit)

How to Change a Commit Message in Git (Last, Old or Pushed Commit)

Sometimes you may commit changes in Git and later realize that the commit message is incorrect, incomplete, or not descriptive enough. Git allows you to modify commit messages using commands such as git commit --amend, git reset, or interactive rebase. In this guide, you will learn how to change commit messages in Git, whether it is the last commit, an older commit, multiple commits, or even a commit that has already been pushed to a remote repository.


Git Change Commit Message Quick Cheat Sheet

CommandDescription
git commit --amend -m "New message"Change the message of the most recent commit.
git commit --amendOpen the editor to modify the last commit message.
git commit --amend --no-editAmend commit while keeping the existing message.
git reset --soft HEAD~1Undo last commit but keep changes staged so you can recommit with a new message.
git reset --soft HEAD^Another way to reset the last commit and rewrite the message.
git rebase -i HEAD~NEdit commit messages for the last N commits using interactive rebase.
git rebase -i <commit-hash>Start interactive rebase from a specific commit to modify messages.
git rebase --continueContinue rebase process after editing commit message.
git push --force-with-leasePush rewritten commit history safely after modifying commit messages.
git reflogView commit history to recover previous commits if something goes wrong.
git log --onelineVerify updated commit messages in a compact format.

Change Last Commit Message

If you just made a commit and realize the message is incorrect or incomplete, Git allows you to update the message of the most recent commit using the --amend option.

Change message using git commit --amend

You can modify the last commit message directly from the command line.

bash
git commit --amend -m "Update configuration handling logic"

This replaces the previous commit message without creating a new commit.

You can also open the default editor to rewrite the message.

bash
git commit --amend

Verify updated commit message

After updating the commit message, verify the change using the commit history with git log.

bash
git log --oneline

This shows the updated commit message in the latest commit.


Change Commit Message After Push

Changing commit messages after pushing to a remote repository is possible, but it rewrites Git history and should be done carefully.

Why changing pushed commits is risky

When you modify a commit message, Git generates a new commit hash. If the commit has already been pushed, rewriting it can cause conflicts for collaborators who already pulled the original commit.

Update pushed commit message using force push

First amend the commit message.

bash
git commit --amend -m "Improve API error handling"

Then force push the updated commit to the remote repository.

bash
git push --force-with-lease

Using --force-with-lease is safer than --force because it prevents accidental overwriting of remote changes.

Safer alternative using revert

If the commit has already been shared with others, a safer approach is to create a new commit that corrects the issue using git revert.

bash
git revert <commit-hash>

This avoids rewriting history while preserving collaboration safety.


Change Commit Message of Older Commit

The git commit --amend command only works for the most recent commit. To edit older commit messages, you must use interactive rebase using git rebase.

Why amend cannot modify older commits

Git maintains commit history as a chain. Since --amend only modifies the latest commit, editing older commits requires rewriting the history using rebase.

Use git rebase interactive to edit commit message

Start an interactive rebase for the last few commits.

bash
git rebase -i HEAD~3

This command opens the editor showing the selected commits.

Reword commit message during rebase

Change the command from pick to reword for the commit whose message you want to modify.

Example:

text
reword 8d12c3f Add configuration file
pick   9c42eaa Update deployment script
pick   a51b2cc Fix environment variable issue

After saving, Git will prompt you to enter a new commit message for the selected commit.


Change Multiple Commit Messages

You can also modify several commit messages at once using interactive rebase.

Start interactive rebase

Run interactive rebase for multiple commits.

bash
git rebase -i HEAD~5

This lists the last five commits in your editor.

Reword multiple commits

Change pick to reword for each commit whose message needs modification.

Example:

text
reword 8d12c3f Add configuration file
reword 9c42eaa Update deployment script
pick   a51b2cc Fix environment variable issue

Git will prompt you to update each commit message sequentially.

Continue rebase safely

After editing messages, continue the rebase process.

bash
git rebase --continue

Finally verify the updated commit history.

bash
git log --oneline

Change Commit Message Without Changing Code

Sometimes you may want to update the commit message without modifying the actual code changes. Git allows you to amend the commit metadata while keeping the existing changes intact.

Use git commit --amend --no-edit

If you want to amend a commit but keep the existing commit message, you can use the --no-edit option.

bash
git commit --amend --no-edit

This command rewrites the commit but keeps the same commit message.

Rewrite message without modifying files

If your goal is to change only the commit message while keeping the code unchanged, run the following command:

bash
git commit --amend -m "Update commit message without changing code"

This replaces the previous message but keeps the same file changes.

You can verify the update using:

bash
git log --oneline

Recover Commit After Message Rewrite

When you amend or rebase commits, Git rewrites the commit history. If something goes wrong, you can recover previous commits using the reflog.

Use git reflog to restore commit history

The git reflog command shows a history of recent HEAD changes and can be explored using .

bash
git reflog

Example output:

text
8f21a9b HEAD@{0}: commit (amend): Update commit message
3c19e7a HEAD@{1}: commit: Add configuration file

Recover previous commit safely

To restore the previous state, reset to the earlier commit using git reset.

bash
git reset --hard HEAD@{1}

This returns the repository to the state before the commit message rewrite.


Git Change Commit Message Examples

Fix typo in commit message

bash
git commit --amend -m "Fix typo in configuration documentation"

Improve commit description

bash
git commit --amend -m "Improve logging logic for API error handling"

Rewrite commit history before pull request

Before opening a pull request, developers often clean up commit messages using interactive rebase.

bash
git rebase -i HEAD~5

This allows you to rewrite commit messages and organize commit history.


git commit --amend vs git rebase

Both commands can modify commit messages, but they are used in different situations.

When to use amend

Use git commit --amend when you want to change the most recent commit message.

Example:

bash
git commit --amend -m "Update commit message"

When to use rebase

Use git rebase -i when you need to modify older commit messages or multiple commits.

Example:

bash
git rebase -i HEAD~3

Differences and risks

Featuregit commit --amendgit rebase
Edits last commit messageYesYes
Edits older commitsNoYes
Modify multiple commitsNoYes
Rewrites commit historyYesYes
Requires force push after remote pushYesYes

Both commands rewrite Git history, so use them carefully when commits have already been pushed to shared repositories.


Frequently Asked Questions

1. How do I change the last commit message in Git?

You can change the most recent commit message using git commit --amend -m "New message". This replaces the previous commit message without creating a new commit.

2. How do I change an older commit message in Git?

To edit older commit messages, use interactive rebase with git rebase -i HEAD~N. Change the action from pick to reword for the commit you want to modify, then save and update the message.

3. Can I change a commit message after pushing to remote?

Yes, but it rewrites Git history. After amending or rebasing, you must force push using git push --force-with-lease. Be cautious when modifying commits that others may have already pulled.

4. What is the difference between git commit --amend and rebase for editing commit messages?

git commit --amend only changes the most recent commit message, while git rebase -i allows you to modify commit messages for older commits in the history.

5. Is it safe to change commit messages in Git?

It is safe for local commits that have not been pushed. However, modifying pushed commits rewrites history and may affect collaborators, so it should be done carefully.

Summary

Git allows you to modify commit messages using several approaches depending on the situation. The git commit --amend command is the easiest way to update the latest commit message, while interactive rebase helps modify older or multiple commits.

Before rewriting commit history, always ensure the commits have not been widely shared or be prepared to use a safe force push strategy. Understanding these techniques helps maintain a clean and meaningful Git commit history. For best practices, see Git Commit Message Guide.


Official Documentation

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.