Webux Lab

By Studio Webux

Changing Global Email in Git

TG
Tommy Gingras Studio Webux 2024-01-13

Changing Global Email in Git: A Step-by-Step Guide

Introduction

When you find the need to replace an email address and/or name associated with the committer or author in a Git repository, itโ€™s crucial to follow a systematic process. This article provides a step-by-step guide on how to globally change the email and name in Git commits. Before proceeding, make sure to back up your entire repository to avoid any unintended data loss.

Important Notes

Before delving into the process, consider the following important notes:

  1. Backup your repository
  2. Write proper conditions: When executing the Git command, ensure that you construct the conditions properly to update only the specific email or name you intend to change.

Step-by-Step Process

Follow these steps to globally change the author and committer information in Git:

1. Set the Email to Change

Define the email address you want to change by assigning it to the EMAIL_TO_CHANGE variable. For example:

EMAIL_TO_CHANGE="contact@studiowebux.com"

2. Execute the Git Filter-Branch Command

Now, execute the following Git filter-branch command, replacing the placeholders with the desired values:

git filter-branch --commit-filter '
    if [ "$GIT_AUTHOR_EMAIL" = "$EMAIL_TO_CHANGE" ] || [ "$GIT_COMMITTER_EMAIL" = "$EMAIL_TO_CHANGE" ];
    then
            GIT_AUTHOR_NAME="New Author Name";
            GIT_AUTHOR_EMAIL="new_author_email@example.com";
            GIT_COMMITTER_NAME="New Committer Name";
            GIT_COMMITTER_EMAIL="new_committer_email@example.com";
            git commit-tree "$@";
    else
            git commit-tree "$@";
    fi' HEAD

This command will globally update both the author and committer information.

3. Push the Changes to Git

git push origin main --force

Sources

For a deeper understanding of Git internals and environment variables, as well as additional tools for rewriting history, refer to the following sources:

By following these steps and considering the provided sources, you can confidently change the email and name associated with commits in your Git repository.


Search