You might want/need to check out repos locally as several different Github users. For example, my Github account is kevinburke, but I push code at work as kevinburkeshyp. If I could only commit/push as one of those users on each machine, or I had to manually specify the SSH key to use, it would be a big hassle!
Generate some SSH keys
First generate SSH keys for each account, and then upload the public keys to the right Github accounts.
ssh-keygen -o -a 100 -t ed25519 -f "$HOME/.ssh/kevinburke_rsa"
ssh-keygen -o -a 100 -t ed25519 -f "$HOME/.ssh/kevinburkeshyp_rsa"
Set up SSH host aliasing
You're going to use your standard SSH config for the host you use more often, and an alias for the one you use less often. Here's my SSH configuration for the host I use more often - on my work laptop, it's my work account.
Host github.com
IdentityFile ~/.ssh/kevinburkeshyp_rsa
HostName github.com
User kevinburkeshyp
Now, set up an alias for your second account.
Host github.com-kevinburke
IdentityFile ~/.ssh/kevinburke_rsa
HostName github.com
User kevinburke
The Host is set to github.com-kevinburke
- this is what you write into your
SSH config locally. The HostName
is what's used for DNS resolution, which is
how this still works. Note you'll want to switch the IdentityFile
and the
User
with the key and usernames you set when you generated the keys.
Now if you want to clone something into the second account, use the special host alias:
git clone git@github.com-kevinburke:kevinburke/hamms.git
That's it! It should just work.
Which Commit Name/Email Address?
Most people configure their commit email address using the following command:
$ git config --global user.email test@example.com
This sets your email address in $HOME/.gitconfig
and applies that value to
every repository/commit on your system. Obviously this is a problem if you have
two different email addresses! I want to use burke@shyp.com
with my Shyp
account and kev@inburke.com
with my personal account.
I fix this by putting an empty email in the global $HOME/.gitconfig
:
[user]
name = Kevin Burke
email = ""
When I try a commit in a new repository, Git shows the following message:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'kevin@rhino.(none)')
Which is good! It prevents me from committing until I've configured my email address. I then have helpers set up to easily configure this.
alias setinburkeemail="git config user.email 'kev@inburke.com'"
That's it! What tips do you use for managing two different Github accounts?
Liked what you read? I am available for hire.
Git 2.13 onward supports conditional includes in .gitconfig which are a useful way to manage user config (e.g., email) per folder hierarchy. https://stackoverflow.com/a/36296990/901597