Makar
Posted on May 29, 2023
Referenced repo: https://github.com/tinkermakar/github-identity-101
TL;DR
Create a separate record for each account in the ~/.ssh/config
file and set user name and email for each repo individually.
The problem
If you have to use multiple git accounts from the same PC, you may make wrong choices (like I did) and end up creating a huge mess with commits by wrong authors in wrong repos all over the place. That's due to the absence of any check on Git's side -- you can claim any identity when pushing commits.
I have been using 2 user accounts for over a year now -- one for work and one for personal projects. During this time I have tried different approaches until I landed on something straightforward, yet safe from human errors.
How to manage git identities
Your number one enemy here is git's global config settings (git config --global user.name ...
). If you are used to set one global git name/email combo, then it's time to unset and forget about that option. Instead, you now need to set your git identity separately in each repo you have cloned by using the same command, but without --global
.
git config user.name John Smith
git config user.email 123123+johnsmith@users.noreply.github.com
If that sounds too complicated, feel free to use this script to automate the process (more on that in the YouTube video above).
How to clone repositories
Before you can even commit anything, you need to clone repos at first place. The best method here is to utilize the SSH config file (~/.ssh/config
). If this is the first time you hear the term SSH, you'd better do some reading first, e.g. starting from here.
In the video above I demo on the example of 2 GitHub users: tinkermakar
and tinkermakar-alt
. I have private SSH keys for both in my .ssh
directory, like so:
├── tinkermakar
│ ├── id_rsa
└── tinkermakar-alt
├── id_rsa
To make use of both at the same time, I need to add a config file with the following content
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/tinkermakar/id_rsa
UserKnownHostsFile ~/.ssh/tinkermakar/known_hosts
PubkeyAcceptedKeyTypes +ssh-rsa
Host alt
HostName github.com
User git
IdentityFile ~/.ssh/tinkermakar-alt/id_rsa
UserKnownHostsFile ~/.ssh/tinkermakar-alt/known_hosts
PubkeyAcceptedKeyTypes +ssh-rsa
Now let's break down the above:
-
HostNae
,User
parameters are mandatory and their value is the same for all GitHub users. -
IdentityFile
parameter is mandatory for our setup and it points at the corresponding private key. - The
UserKnownHostsFile
is not mandatory -- it's a matter of preference on whether you want yourknown_hosts
strings separate (as in the example) or all in one place. -
PubkeyAcceptedKeyTypes
property has recently become required for some systems, so keeping it there is probably a good idea.
you can test both ssh configurations by running:
ssh git@github.com
ssh alt
Running the two above will result in the generation of known_hosts
files, and the final structure of your .ssh
directory will look similar to this:
├── config
├── tinkermakar
│ ├── id_rsa
│ ├── id_rsa.pub
│ └── known_hosts
└── tinkermakar-alt
├── id_rsa
├── id_rsa.pub
└── known_hosts
Now you can git clone repos on behalf of both git users, but there is one caveat -- you may need to alter the URI of cloned repository based on the value of your SSH Host (the string right next to Host
, which is the first line for each identity in the ~/.ssh/config
file). In the example above, hosts are equal to github.com
and alt
. Since Host and HostName match for the former, it can clone with no need for alterations:
git clone git@github.com:tinkermakar/github-identity-101.git
However, the second user cannot do the same -- git clones on the latter's behalf must replace git@github.com
with the value of Host
, which is alt
in our case.
git clone alt:tinkermakar/github-identity-101.git
Recap
So if you need to use multiple GitHub accounts from the same PC, then I strongly advise you to take the following steps:
- Generate and store 2 separate pairs of RSA keys
- Draft a
config
file to point at each separately - When cloning repos, alter the beginning of SSH strings with the value of
Host
for the user you want to use - Set your git name and email separately for each repo (feel free to use this script to streamline the process).
Posted on May 29, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.