Let’s Find the Problem First
I have two GitHub accounts—one for personal use and another for work. I want to use both accounts on the same computer without repeatedly entering passwords during git pull and git push operations.
Solution
We can achieve this by configuring SSH keys and defining multiple GitHub hosts in an SSH config file.
1. Generate SSH Keys for Both Accounts
Run the following commands to generate SSH keys for your personal and work accounts:
ssh-keygen -t ed25519 -C "[email protected]"
ssh-keygen -t ed25519 -C "[email protected]"
This will create SSH keys using your email addresses as labels.
Save SSH Keys with Custom Names
When prompted to specify the file path, rename the files for clarity:
/Users/username/.ssh/kishanjasani
/Users/username/.ssh/kishancompanyname
2. Create or Edit the SSH Config File (~/.ssh/config)
If the SSH config file doesn’t exist, create it with:
touch ~/.ssh/config
Now, open the file in an editor and add the following configurations:
# Personal GitHub Account.
Host github.com
HostName github.com
IdentityFile ~/.ssh/kishanjasani
IdentitiesOnly yes
# Work GitHub Account.
Host github-kishancompanyname
HostName github.com
IdentityFile ~/.ssh/kishancompanyname
IdentitiesOnly yes
This setup tells SSH which key to use when connecting to different GitHub accounts.
3. Add SSH Keys to Your SSH Agent
Run these commands to add your private keys to the SSH agent:
ssh-add ~/.ssh/kishanjasani
ssh-add ~/.ssh/kishancompanyname
4. Test Your Connection
Verify that the SSH keys are working by running:
ssh -T [email protected]
ssh -T git@github-kishancompanyname
If prompted with:
The authenticity of host ‘github.com’ can’t be established…
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Type yes. If everything is set up correctly, you’ll see messages like:
Hi kishanjasani! You’ve successfully authenticated, but GitHub does not provide shell access.
5. Clone a Repository Using the Work GitHub Account
To clone a repository from your work GitHub account, use:
git clone git@github-kishancompanyname:organisation/project2.git
git config user.email "[email protected]"
git config user.name "Kishan Companyname"
Conclusion
That’s it! You can now seamlessly push and pull from multiple GitHub accounts on the same computer without switching credentials manually. 🚀
Leave a Reply