RSSAmplifier

fernvenue's Blog · Jun 8, 2024

How to edit history Git commit message?

0
Sign in to vote or save

fernvenue's Blog

When dealing with various repositories and branches, it is sometimes inevitable that we make mistakes. Additionally, there are times when looking back at previous commit histories, we may wanna add more details to make it easier for other collaborators to review. Alternatively, we may have changed email or other information and want to modify the information in historical commits to make it easier for others to contact us when they encounter issues. For these and various other reasons not listed here, editing Git commit history becomes a likely scenario. Here are two examples to illustrate how to edit commit history and the committer information in historical commits.

Change Git History Commit Message

First, let’s assume a simple scenario where we need to edit a commit record from three commits ago.

git rebase -i --rebase-merges HEAD~3

Here, the last three commits will appear:

pick b2473db Some commit message here.
pick 2e10a08 Some other commit message here.
pick 488d992 Here the commit message we wanna edit.

Suppose we want to modify the commit message corresponding to 488d992, just change its pick to reword:

pick b2473db Some commit message here.
pick 2e10a08 Some other commit message here.
reword 488d992 Here the commit message we wanna edit.

After saving, you will enter the window to modify the commit message. Here, you can edit the commit message. After editing and saving, let’s check the modifications:

git log

If there are no issues, force push to the repository:

git push --force

This completes the modification. By the way, we added --rebase-merges in the first step, which is essential, otherwise, merge-related records and information will be lost during the editing process.

Change Historical Committer Information

First, use git log to view historical commit information and find the email and name you wanna change. Then, let’s use a simple script to update it:

#!/bin/bash

git filter-branch --env-filter '
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"

if [ "$GIT_COMMITTER_EMAIL" = "$EXPIRED_EMAIL" ]
then
    cn="$CURRENT_NAME"
    cm="$CURRENT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$EXPIRED_EMAIL" ]
then
    an="$CURRENT_NAME"
    am="$CURRENT_EMAIL"
fi

export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'

Replace $EXPIRED_EMAIL in the script with the email address you want to modify in the Git commit history. Then, change $CURRENT_NAME and $CURRENT_EMAIL to the target content. Run the script and confirm:

chmod +x ./fix.sh && ./fix.sh
git log

If there are no issues, force push to the repository:

git push --force

This completes the modification.

Links

Read the original on blog.fernvenue.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.