Hide your email address on GitHub

When you create a commit on git, the username and email is associated with the commit. So when someone clones the repository, they can see the list of commits and the associated user. Thus, you can get a list of email address. People often use their private email address, so you can be spam. For instance, here's the output of git log on the repo corefx. You can see some email address

Configure git

GitHub provides a noreply address for every user. For instance, my email is meziantou@users.noreply.github.com. You can find it on the settings page: https://github.com/settings/emails.

Then, you can change your git configuration:

git config --global user.email meziantou@users.noreply.github.com
git config --global user.name meziantou

If you want to configure only a single repo,

cd "path to the git repository"
git config user.email meziantou@users.noreply.github.com
git config user.name meziantou

Rewrite history

Then, you want to replace your email in the previous commits. git provides a functionality to rewrite the history of the repository. If you are on Windows, I advise you to use bash as the escape characters are not the same on both system. If you have not yet configured bash, read this documentation.

git filter-branch --commit-filter '
    if [ "$GIT_COMMITTER_EMAIL" = "<Your old email address>" ];
    then
        GIT_COMMITTER_NAME="<Your name>";
        GIT_AUTHOR_NAME="<Your name>";
        GIT_COMMITTER_EMAIL="<Your noreply email address>";
        GIT_AUTHOR_EMAIL="<Your noreply email address>";
        git commit-tree "$@";
    else
         git commit-tree "$@";
    fi' HEAD

Then, you have to push your changes to the remote repository. You must use --force to overwrite the remote repository.

git push --force

Change GitHub settings to block commits containing your email address

Finally, you can configure GitHub to block commits that contains your actual email address. Go into the settings / email section and check the box Block command line pushes that expose my email

Leave a reply