August 26, 2021 | Less than 1 minute to read
Juggling multiple Git identities can be tricky.
For example, at Stripe, we encourage developers to create a separate GitHub account for Stripe-related open source activity. For me, this means I now own both a
nfriend and a
nfriend-stripe GitHub profile.
While setting up my dev environment, I had a few goals:
Good news! This is possible with a little
.gitconfig magic ✨
Note: The instructions below rely on Git’s conditional includes, which are only available in Git 2.13 and beyond.
I won’t go into details since this is already covered in great detail by other tutorials. GitHub’s tutorials are particularly well-presented:
For example, a
~/github-personal and a
.gitconfig_includefile in each
Inside each of these new directories, create a new file named
.gitconfig_include with the following content:
[user] name = Your Name email = email@example.com signingkey = 0123456789ABCDEF [core] sshCommand = ssh -i ~/.ssh/id_rsa_example -F /dev/null
Update each file with the name, email, and signing key for the corresponding Git identity.
Additionally, update the command in the
sshCommand option to reference the appropriate key file.
In your global
~/.gitconfig), configure Git to conditionally include the correct
.gitconfig_include file based on the current directory:
[includeIf "gitdir:~/github-personal/"] path = ~/github-personal/.gitconfig_include [includeIf "gitdir:~/github-work/"] path = ~/github-work/.gitconfig_include
Create a test project with both identities. Ensure you can:
If you’re using a web UI like GitLab or GitHub, check to see that your commits are being signed correctly and are labeled as “Verified”:
Some things I found helpful while setting this up:
.gitconfigsettings on a per-directory basis: https://stackoverflow.com/a/48088291/1063392
core.sshCommandconfiguration to select an SSH key: https://superuser.com/a/912281/144803
Thoughts? Let me know in this GitLab issue!
Other posts you may enjoy:
May 7, 2021 | 1 minute to read
November 18, 2020 | 6 minutes to read
October 19, 2019 | 2 minutes to read
April 15, 2019 | 6 minutes to read
December 4, 2018 | Less than 1 minute to read