📅  最后修改于: 2023-12-03 14:41:25.257000             🧑  作者: Mango
As a developer, you may have faced a situation where you need to change the author of all your Git commits in a repository. This could be due to various reasons, such as a change in your email address or username. Changing the author of all commits in a repository can be a tedious task, especially if you have a large number of commits. In this guide, we will show you how to change the author of all commits in a Git repository using Shell/Bash scripting.
Before you start, ensure that you have the following:
$ git clone <repository_url>
$ cd <repository_name>
change-author.sh
in the root directory of the repository.$ touch change-author.sh
change-author.sh
file in a text editor and add the following script:#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="<old_email>"
CORRECT_NAME="<correct_name>"
CORRECT_EMAIL="<correct_email>"
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
Note: Replace <old_email>
, <correct_name>
and <correct_email>
with your old email, correct name and correct email respectively.
change-author.sh
file and make it executable.$ chmod +x change-author.sh
change-author.sh
script.$ ./change-author.sh
$ git log --pretty=full
You should see the updated author information for all commits.
In this guide, we have shown you how to change the author of all commits in a Git repository using Shell/Bash scripting. This method is useful when you need to change the author of all your commits in bulk. However, you should use this method with caution, especially if the repository is shared with others. It is recommended to inform the other contributors of the change in author information.