ENTRY

[ESC]
1hW:719S:1R:4

A guide on how to use multiple git accounts

this guide is mostly written for me so i don't have to scramble and piece together how i did it after a few months of not doing anything git related, but for the sake of sharing things people already know, i decided to write a blog for it instead :D

i have so many accounts for github mainly for work and some other personal endeavors whatever curiosity i have at that time so it is a bit frustrating to switch between multiple accounts whenever i'm not working on some corporate project.

this guide assumes the reader (me or somebody else) knows how to use bash or ssh so i'm going to skip some core concept theory; well it's a guide that looks like a cookbook.

assume that you have a new computer or you decided to just foobar your existing ssh setup, we'll need to create the ssh keys for the primary and secondary ssh keys for each account that you have.

ssh-keygen -t ed25519 -C "primary@email.com

then for your second or other accounts you can just write it as.

ssh-keygen -t ed25519 -C "secondary@email.com" -f ~/.ssh/id_ed25519_second

we can go crazy with how many ssh keys as much as we like but just keep track of their ssh records we created prior, so name them appropriately.

next up is adding the ssh keys to your keychain, i'm using a macbook for this since it's what i mostly carry around and my desktop machines are mostly used for gaming anyway XD

we must first run the agent eval "$(ssh-agent -s)" .

then add the keys to the keychain.

ssh-add --apple-use-keychain ~/.ssh/id_ed25519_second

ssh-add --apple-use-keychain ~/.ssh/id_ed25519 this is the primary ssh key that we added earlier

or if you use linux btw

ssh-add ~/.ssh/id_ed25519

ssh-add ~/.ssh/id_ed25519_second

now that we have those keys in place we can then paste them to our github accounts.

pbcopy < ~/.ssh/id_ed25519.pub for mac

xclip -selection clipboard < ~/.ssh/id_ed25519_second.pub for linux

next up is adding it to the ssh config file, don't ask me how they work since i have no clue.

# the first git account

Host github.com

HostName github.com

User git

IdentityFile ~/.ssh/id_ed25519

# second git account

Host github.com-second

HostName github.com

User git

IdentityFile ~/.ssh/id_ed25519_second

# add more entries if you feel like two accounts aren't enough for world domination

sometimes i'm being paranoid when following guides like this so i run ssh -T git@github.com-second

to see if i did the setup properly.

if it's an already cloned project we just need to point it to the correct ssh key that we added to the config.

git remote set-url origin git@github.com-second:username/repo.git

we can then do the pushing of commit as

git remote set-url origin git@github.com:username/repo_name.git

or as your secondary account

git remote set-url origin git@github.com-second:username/repo_name.git

then push it as normal git push -u origin main

for pulling changes we can just roll with git pull

run git remote -v in the terminal, you will see two lines for origin—one for (fetch) and one for (push)

origin git@github.com-second:username/repo_name.git (fetch)

origin git@github.com-second:username/repo_name.git (push)

as long as the (fetch) URL contains the correct alias from your ~/.ssh/config file, your pulls will route through the correct account seamlessly.

okay now we have those setup, we can now enjoy working on our personal projects without accidentally committing as our personal accounts to our work company's github repo and possibly alerting our bosses that we're working at other projects during work hours XD

there's also a hacky way of automatically binding which account it's supposed to point at when cloning

i haven't tried this with more than two accounts so ymmv

the ssh config file alone cannot do this. ssh only sees the domain name (github.com) when it connects. it has no idea which repository path (primary/... vs secondary/...) we are trying to access until after authentication is finished, so it cannot choose the key based on the repository name.

we can achieve this exact seamless behavior by using git's url rewriting feature You can tell git to automatically intercept any request for the secondary namespace and swap in your github.com-second alias behind the scenes.

let's use an example of what my current config looks like

git config --global url."git@github.com-second:sckickar/".insteadOf "git@github.com:sckickar/"

that's all for the blog, hopefully this will help someone out or smth but if not then excuse my yapping :D

4 replies

Log in to read the replies and join the conversation