If you sometimes work on both personal and professional projects that are hosted on GitHub, chances are that you’d like to separate identities. Maybe you have several accounts, maybe it’s just a matter of making the commits use different emails.
In this post, we’ll see how to manage such separate identities, at the SSH level and the Git level.
Configuring Per-Repository Author Information in Git
This requires Git 2.13 or above
The idea is to organize cloned repositories in a hierarchy of folders, which
will automatically get the correct configuration thanks to the Conditional
Thanks to that feature, one can alter the configuration depending to a pattern, e.g. on the absolute path of a given repo. That can be put to use to alter the authorship information for instance.
Define such a pattern inside the
.gitconfig file in your home folder:
[user] name = Bruce Wayne email = <email@example.com> [includeIf "gitdir:~/BatCave/"] path = .gitconfig-secret
Then define the overriding configuration of the
[user] name = Batman email = <firstname.lastname@example.org>
Now, every time you clone a Git repository under the
BatCave base folder, Git
will use your secret batman identity for your commits 😎 👍
This local configuration is visible with a
git config --list IF you’re
actually inside a git-enabled folder.
Configuring Different GitHub Accounts Via SSH Host Configuration
The next step is when you have different GitHub accounts (e.g. your work requires you to have a dedicated account, and you have personal pet projects that you manage using a different account).
Chances are, you’d like to authenticate to these separate accounts using SSH. For this, you need a little bit of setup, both for SSH and Git.
We’ll assume that you have generated two SSH keys:
to the adequate GitHub accounts.
First let’s setup SSH with Host configuration. In your
config file (if it doesn’t already exists).
In there, put domain-specific configurations like these two:
#work SSH identity (default) Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_github #personal SSH identity Host batman.github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_githubbatman
Let’s dive into a bit more details on the second configuration:
Host batman.github.comis the key: SSH will recognize this “virtual” domain, e.g. in a
git remoteURL, and associate this configuration to it…
- …but it will call the actual
github.comdomain, as configured by the
- The user is still traditionally
gitin SSH git remotes
IdentityFileis the SSH key to use.
Next you still need to tell
git to use that SSH identity. For this, you need to
edit your git SSH remote(s) in order to replace the
github.com domain in the
git remote set-url origin email@example.com:batman/batcave-OS.git
Think of it as a kind of DNS for Git 😄