How Do I Change the Author and Committer Name/Email for Multiple Commits?

Better Stack Team
Updated on June 24, 2024

To change the author and committer name/email for multiple commits, you can use the git filter-branch command with the --env-filter option. This allows you to rewrite commit history by modifying environment variables.

First, create a script that updates the author and committer information. Open your text editor and create a script, for example, change-author.sh, with the following content:

 
#!/bin/bash

git filter-branch --env-filter '

OLD_EMAIL="old-email@example.com"
CORRECT_NAME="New Author Name"
CORRECT_EMAIL="new-email@example.com"

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

Replace old-email@example.com with the old email address, New Author Name with the new author name, and new-email@example.com with the new email address.

Make the script executable using the following command:

 
chmod +x change-author.sh

Execute the script to rewrite the commit history:

 
./change-author.sh

After rewriting the commit history, force-push the changes to the remote repository:

 
git push --force --tags origin 'refs/heads/*'

Note:

  • Rewriting commit history can cause issues if the commits have already been pushed to a shared repository and other collaborators have based their work on them. Use it with caution.
  • After rewriting commit history, collaborators who have pulled the changes will need to rebase their work to incorporate the updated history.
  • Consider communicating any changes to other collaborators to avoid confusion.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.