Edit author/committer of multiple commits based on commit message

46 Views Asked by At

I am looking to edit the author/committer of several commits whose message is prefixed with [TEST].

I know that you can edit the authors with environment variables but I can't find it by searching with the commit message.

git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Useername"
CORRECT_EMAIL="[email protected]"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

We should be able to integrate a regex like this "[TEST]*":

if commit message = "[TEST]*" then 
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
1

There are 1 best solutions below

1
Guildenstern On BEST ANSWER

Using the third-party git-filter-repo(1):

git filter-repo --commit-callback='
if commit.message.startswith(b"[TEST]") and commit.author_email == b"[email protected]":
    commit.committer_name = b"Username"
    commit.committer_email = b"[email protected]"
    commit.author_name = b"Username"
    commit.author_email = b"[email protected]"
'

This Python callback (see manual) lets you change the commit in-place.

You will need to pass --force as well in order to do the change because this is a destructive update that cannot (in general) be undone.

git-filter-branch(1)

git-filter-branch(1) is effectively deprecated and no one that I've seen has recommended its usage.