Open In App

How To Modify Existing, Unpushed Commit Messages in Git?

Last Updated : 13 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When using Git, it's easy to make mistakes or want to change commit messages after you've made them. If you haven't pushed your changes to a remote repository yet, you can edit the commit message without messing up the commit history. In this article, we'll look at different ways to change commit messages that haven't been pushed yet.

Approach 1: Using git commit --amend

The --amend option allows you to modify the most recent commit message. When you run git commit --amend, Git will create a new commit that replaces the previous one, using the same changes, but with a new commit message.

Step 1: Git creates a new commit with the same changes as the previous one.

Step 2: The new commit gets a new commit hash.

Step 3: The previous commit is no longer referenced by the branch.

Step 4: The branch pointer is updated to point to the new commit.

# Create a new commit with a mistake in the message
git commit -m "Initial commit with a typo"

# Modify the commit message using --amend
git commit --amend -m "Initial commit without typo"

Output

Capture
Using git commit --amend

Approach 2. Using git rebase -i

The rebase -i option allows you to interactively modify commit messages. When you run git rebase -i, Git opens an interactive shell where you can modify the commit messages, reorder commits, or even delete commits.

Step 1: Git creates a temporary branch that points to the commit you want to modify.

Step 2: Git opens an interactive shell with a list of commits, where you can modify the commit messages.

Step 3: You can reorder commits, delete commits, or modify commit messages.

Step 4: When you save and close the file, Git applies the changes to the temporary branch.

Step 5: The temporary branch is then merged into your original branch.

# Create multiple commits with mistakes in the messages
git commit -m "Commit 1 with typo"
git commit -m "Commit 2 with typo"
git commit -m "Commit 3 with typo"

# Modify the commit messages using rebase -i
git rebase -i HEAD~3

This will open an shell where you can modify the commit messages. Replace pick with edit for the commits you want to modify, and then save and close the file.

edit 1234567 Commit 1 with typo
edit 2345678 Commit 2 with typo
edit 3456789 Commit 3 with typo

Then, for each commit, run:

git commit --amend -m "new commit message"
git rebase --continue

Output

Capture
Using git rebase -i

Approach 3. Using git filter-branch

The filter-branch option allows you to rewrite commit messages using a custom command. When you run git filter-branch, Git applies the command to each commit message, rewriting the commit history.

Step 1: Git creates a temporary branch that points to the commit you want to modify.

Step 2: Git applies the custom command to each commit message, rewriting the commit history.

Step 3: The temporary branch is then merged into your original branch.

# Create multiple commits with mistakes in the messages
git commit -m "Commit 1 with typo"
git commit -m "Commit 2 with typo"
git commit -m "Commit 3 with typo"

# Modify the commit messages using filter-branch
git filter-branch --msg-filter 'sed "s/typo/corrected/"' HEAD~3

This will rewrite the commit messages, replacing "typo" with "corrected".

Output

Capture
Using git filter-branch

Next Article
Article Tags :

Similar Reads