A C# Developer’s Guide To Keeping You Email Private and Out Of GitHub Commits


If you’re about to start working with public repos on GitHub, you might want to hide your email address(es) from the commit history.

Step 1 (Find the private email)

GitHub website -> Settings -> Emails.

Scroll down to ‘Keep my email address private’ and you’ll be able to take a copy of your no-reply address that will look something like :1119252+foobar@users.noreply.github.com

Note that if you’re missing the number prefix commits made with that no-reply email won’t be associated with you if you change your username. Simply un-check then re-check the ‘Keep my email address private’ box and you’ll get a new one with the id prefix.

Step 2 (Activate on GitHub website)

Enable it for any website operations such as creating a new repo with an initial commit.

Simply click the checkbox next to ‘Keep my email address private’.

Step 3 (Local setup)

Note use of Windows paths in this example.

It’s good idea if your global config i.e. c:\users\<username>\.gitconfig has your default user for new git providers. You can then set conditions depending on root working directory.

So I might have c:\teamgit for working with our team’s on-prem Azure DevOps git provider and c:\github for working on GitHub. You can break this down as much as you need.

Contents of .gitconfig:

It’s really picky about that trailing slash and the /i gets around case issues. It’s a beast that wants to live on UNIX but is forced into Windows 🙂 At least the file names and extensions can be anything you want.

Contents of github-user.gitconfig:

Contents of teamgit-user.gitconfig:

This works in command line, VS Code and Visual Studio. In the latter it won’t acknowledge it in Team Explorer git settings (for sensible reasons) but you’ll see it is working in your commit log.

You could instead do this email config at repository level, which is a pain to remember. There is a git config option to have it ask you every new clone but I prefer the above as a more robust solution.

Don’t forget to ask anyone you add as a co-author to a commit if they would prefer to use their noreply email – they’ll need to give you this!

Testing it works

You’ll want to try making some commits but before you do it is straightforward to check with the following:

For the repo you’r sitting in, the show-origin bit will let you see precisely which config file the email taken from.

A gotcha

You’ll have seen a second option in the email settings on GitHub:

Block command line pushes that expose my email

This is another layer of safety but with it enabled you will get an error on pushing a new branch where somewhere in the history, one of your private emails is exposed in a commit.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.