2 August 2018
Git with multiple accounts
Consider scenario, where you have two Git accounts on your machine, and want to manage different Github accounts. I assume that you have installed GIt, and created Gihub accounts. You can check my short post about using git with Visual Studio Code: Git things with VSCode.
These are steps which I take in order to accomplish this:
Set up SSH Keys
$ cd ~/.ssh $ ssh-keygen -t rsa -C "email1@domain.com" # save it as id_rsa_1 $ ssh-keygen -t rsa -C "email2@domain.com" # save it as id_rsa_2
Add the keys to your Github accounts
Go to “Account Settings”/ “SSH Keys”/ “Add SSH key”
Create a configuration file to manage the separate keys
Create a ‘config’ file in ~/.ssh/:
# Default GitHub Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_1 # 2nd GitHub Host github.com-2 HostName github.com User git IdentityFile ~/.ssh/id_rsa_2
Update stored identities
To automatically load them every time when Bash starts you can edit ‘.bash_profile’ file:
eval $(ssh-agent -s) ssh-add ~/.ssh/id_rsa_1 ssh-add ~/.ssh/id_rsa_2
Choose account
Taking action in Git using second account:
Cloning:
git clone git@github.com-2:UserName/repo.git
Adding
First you have to define user, for convenience on local level:
git config --local user.name "UserName" git config --local user.email "email2@domain.com"
Things to remember:
- Github recognises users by e-mail.
- Each level of Git config (system, global, local) overrides values of the previous one.