credit: jcmiron
i am trying to add both my work and personal GitHub account to same
device.
so the steps involved are:
-
enable and start (if not all ready) OpenSSH Authentication Agent. you can do it from the services or by running command below
# start the ssh-agent in the background Get-Service -Name ssh-agent | Set-Service -StartupType Manual Start-Service ssh-agent
create ssh key at local machine for each account (here work and personal). follow this link for in depth explanation create ssh.
-
add a config file in the same directory as .ssh folder from step 1. in my case, the content of this config file is:
# Work GitHub Host work # alias for work Hostname github.com User git IdentityFile ~/.ssh/work_key # private key for work IdentitiesOnly yes # Personal GitHub Host per # alias for personal Hostname github.com User git IdentityFile ~/.ssh/personal_key # private key for personal IdentitiesOnly yes
add ssh key to your github account. follow add ssh to github. basically cat your personal key (make sure it is not accessible for all users, i had to revoke permission for all other users) and copy the content till last (with out carriage return) and paste it in GitHub.
-
add both private keys to OpenSSH cache by running following command
ssh-add .\work_key ssh-add .\personal_key
-
confirm things are working so far by running
ssh -Tv per # for personal key (per is the alias) ssh -Tv work # for work key (work is the alias)
-
if everything is correct so far, you will see the line below in the pile of lines
Hi anmjubaer! You've successfully authenticated, but GitHub does not provide shell access.
(in your case, your name)
-
next, go to your user folder. you may see a file named .gitconfig. if not, create one with content (with others if exists)
[includeIf "gitdir:E:/work/"] # maintain seperate work repo-parent folder (where all your work-related repositories will be located) path = ./.gitconfig-work [includeIf "gitdir:E:/jb/"] # maintain seperate personal repo-parent folder (where all your personal repositories will be located) path = ./.gitconfig-personal [user] name = jubaerad email = jubaerad1@gmail.com [core] sshCommand = C:/Windows/System32/OpenSSH/ssh.exe
don't worry, we will create .gitconfig-personal and .gitconfig-work next
-
now create two more file named (.gitconfig-work and .gitconfig-personal if you want) with content
for .gitconfig-work
[user] name = anmj email = yourworkemail@email.blah [core] sshCommand = "ssh -i ~/.ssh/work_key"
for .gitconfig-personal
[user] name = jubaerad email = jubaerad1@gmail.com [core] sshCommand = "ssh -i ~/.ssh/personal_key"
-
now clone or set remote starting with appropriate host alias. for example, instead of
git clone git@github.com:jubaer-ad/GraphQLDemo.git
use
git clone per:jubaer-ad/GraphQLDemo.git # remember? per is the alias we set earlier for personal usage
finally some words from Gemini 2.0 Flash (yes, i used it)
"Boom! You've officially wrangled those SSH keys into submission. Now you can switch between GitHub accounts like a secret agent changing disguises. Just remember, with great SSH power comes great responsibility... like, don't accidentally push your personal cat meme collection to your work repo. We've all been there. (Or, at least, I've heard someone has.) Happy coding, and may your pushes be swift and your merges be conflict-free (a developer can dream, right?)."
Top comments (0)