Setup Multiple Git Accounts On local System
To activate your SSH key for use with a remote Git repository, you typically need to perform the following steps:
(Here i am explaining with 2 example emails)
Generate SSH Key Pair: If you haven't already done so, generate an SSH key pair on your local machine. You can do this using the
ssh-keygen
command. Make sure to provide a passphrase when prompted for added security.case1: ssh-keygen -t rsa -b 4096 -C "work@example.com" case2: ssh-keygen -t rsa -b 4096 -C "personal@example.com"
Store file in your required folder
Example::case1:: Enter file in which to save the key (/Users/mylaptop/.ssh/id_rsa): /Users/mylaptop/.ssh/git-accounts/work_id_rsa case2:: Enter file in which to save the key (/Users/mylaptop/.ssh/id_rsa): /Users/mylaptop/.ssh/git-accounts/personal_id_rsa
Add SSH Key to SSH Agent: Add your SSH private key to the SSH agent running on your local machine.
case1: ssh-add ~/.ssh/git-accounts/work_id_rsa case2: ssh-add ~/.ssh/git-accounts/personal_id_rsa
Copy Public Key to Remote:
Copy the contents of your SSH public key (
~/.ssh/
git-accounts/work_id_rsa.pub
& ~/.ssh/
git-accounts/personal_id_rsa.pub
) to the remote server or Git hosting service where you want to use the key. This typically involves adding the public key to your user's SSH authorized keys list.For example, if you're using GitHub, you can go to your account settings, then SSH and GPG keys, and add a new SSH key with the content of above files(
~/.ssh/
git-accounts/work_id_rsa.pub
& ~/.ssh/
git-accounts/personal_id_rsa.pub
).case1: cat ~/.ssh/git-accounts/work_id_rsa.pub case2: cat ~/.ssh/git-accounts/personal_id_rsa.pub
copy the code and add a new ssh key in the remote
Configure SSH Config File: Create or edit the
~/.ssh/config
file to specify different SSH identities for different Git hosts. Here's an example of how you can configure it:( create config file in .ssh folder)# Work account Host github.com-work HostName github.com User git IdentityFile ~/.ssh/git-accounts/work_id_rsa # Personal account Host github.com-personal HostName github.com User git IdentityFile ~/.ssh/git-accounts/personal_id_rsa
Test SSH Connection: Test the SSH connection from your local machine to the remote server or Git hosting service to ensure that authentication works correctly.
1. ssh -T git@github.com-work 2. ssh -T git@github.com-personal
Clone git repo using remote ssh url
1. git clone git@github.com-work:username/repository.git 2. git clone git@github.com-personal:username/repository.git
Update Git Remote URLs: In each local repository, update the remote URLs to use the configured SSH aliases.
1. git remote set-url origin git@github.com-work:username/repository.git 2. git remote set-url origin git@github.com-personal:username/repository.git
Configure Git Identity: Optionally, you can configure Git to use different identities (name and email) for each repository by setting local Git configuration. In the repository directory, run:
git config user.name "Your Name" git config user.email "work@example.com"
Make sure to replace placeholders(points 7, 8 & 9) like
username
,repository
,Your Name
, and work@example.com
with your actual information.Once you've completed these steps,you can maintain different Git accounts for different repositories on your local system. your SSH keys should be set up and ready to use with your Git hosting service. You can now clone repositories, push commits, and perform other Git operations using SSH authentication.