How to change multiple emails in Your Repos

---
date: Jul 13, 2016
tags:
- GitHub
- Shell
language: English 正體中文
---
This post is more than 6 years old. If this is a technical post, the post will most likely not working, but feel free to try it and see if it works.

Recently, I am programming an app on GitHub (Yes, it is WeCode as you have guessed), but I suddenly realized the commits are not properly shown in my dashboard:

GitHub Dashboard
GitHub Dashboard

As a GitHuber, I hate that not all my commits are not shown. As I browse the Git History in Visual Studio, I find that Microsoft changed my email address:

Well Done, Microsoft
Well Done, Microsoft

Well done, Microsoft.

GitHub actually provides you with a feature to change author info . However, I found it misleading and confusing to the newbies, while only support only one email address. So I decided to write a new instruction for newbies for better understanding.

Changing author info

To change the name and/or email address recorded in existing commits, you must rewrite the entire history of your Git repository.

This action is destructive to your repository’s history. If you’re collaborating on a repository with others, it’s considered bad practice to rewrite published history. You should only do this in an emergency.

Changing the Git history of your repository using a script

We’ve created a script that will change any commits that previously had the old email address in its author or committer fields to use the correct name and email address.

Running this script rewrites history for all repository collaborators. After completing these steps, any person with forks or clones must fetch the rewritten history and rebase any local changes into the rewritten history.

Before running this script, you’ll need:

Instruction:

  1. Open Git Bash.

  2. Create a fresh, bare clone of your repository:

    1
    2
    git clone --bare https://github.com/*user*/*repo*.git
    cd *repo*.git
  3. Copy and paste the script in a text editor, replacing the following variables based on the information you gathered:

    • OLD_EMAIL
    • CORRECT_NAME
    • CORRECT_EMAIL
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    #!/bin/sh
    # see https://help.github.com/articles/changing-author-info/

    git filter-branch --env-filter '

    OLD_EMAIL=(
    "your-old-email@example.com"
    "another-old-email@example.com"
    "your-git-email@users.noreply.github.com"
    )
    CORRECT_NAME="Your Correct Name"
    CORRECT_EMAIL="your-correct-email@example.com"

    for NEW_EMAIL in ${OLD_EMAIL[@]}; do
    if [ "$GIT_COMMITTER_EMAIL" == "$NEW_EMAIL" ]; then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    fi
    if [ "$GIT_AUTHOR_EMAIL" == "$NEW_EMAIL" ]; then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
    done
    ' --tag-name-filter cat -- --branches --tags
  4. Save the script in the repository folder just created as git-author-rewrite.sh.

  5. In Git Bash, run following code:

    1
    sh git-author-rewrite.sh
  6. Review the new Git history for errors.

  7. Push the corrected history to GitHub:

    1
    git push --force --tags origin 'refs/heads/*'
  8. Clean up the temporary clone:

    1
    2
    cd ..
    rm -rf *repo*.git

Comments

Navigation