📜  git change autor of all comits - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:41:25.257000             🧑  作者: Mango

Git change author of all commits - Shell/Bash

Introduction

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.

Prerequisites

Before you start, ensure that you have the following:

  • Basic understanding of Shell/Bash scripting
  • Git installed on your system
Steps
  1. Clone the Git repository where you want to change the author of all commits.
$ git clone <repository_url>
  1. Navigate to the root directory of the repository.
$ cd <repository_name>
  1. Create a Shell/Bash script file named change-author.sh in the root directory of the repository.
$ touch change-author.sh
  1. Open the 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.

  1. Save the change-author.sh file and make it executable.
$ chmod +x change-author.sh
  1. Run the change-author.sh script.
$ ./change-author.sh
  1. Verify that the author of all commits has been changed by running the following Git command.
$ git log --pretty=full

You should see the updated author information for all commits.

Conclusion

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.