Skip to content

Instantly share code, notes, and snippets.

@colormono
Last active May 28, 2020 21:57
Show Gist options
  • Save colormono/ee026d7b74dda6a7fb96f5680dec04a0 to your computer and use it in GitHub Desktop.
Save colormono/ee026d7b74dda6a7fb96f5680dec04a0 to your computer and use it in GitHub Desktop.
Multiple git accounts, same computer #setup #CLI

Follow below steps to add multiple accounts. More info at: https://gist.github.com/jexchan/2351996

Step 1:

goto .ssh folder and generate ssh keys for all your github accounts

$ cd ~/.ssh

$ ssh-keygen -t rsa -b 4096 -C "personal_email_id"

save as id_rsa_personal

$ ssh-keygen -t rsa -b 4096 -C "work_email_id"

save as id_rsa_work

Step 2:

Copy id_rsa_personal.pub and id_rsa_work.pub and add to respective github account.

Step 3:

Create config file in .ssh folder And add below configs

$ touch config

# Personal account - default config
Host github.com-personal
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal
   
# Work account
Host github.com-work
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work

Step 4:

create .gitconfig for personal and work directory with respective config git host names

$ cd ~

$ nano ~/.gitconfig

[user]
    name = personal_name
    email = personal_email_id
[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig

$ nano ~/work/.gitconfig

[user]
    name = work_name
    email = work_email_id

Step 5:

Add new ssh keys

$ cd ~/.ssh
$ ssh-add id_rsa_personal`
$ ssh-add id_rsa_work
$ ssh-add -l

Step 6:

Check configuration is right by pinging to github with below commands

$ ssh -T github.com-personal $ ssh -T github.com-work

Step 7:

Always clone repo by adding hostname in remote url e.g. git@github.com to git@github.com-personal

git clone git@github.com-personal:colormono/colormono.github.io.git
git@github.com-work:truenorth/multiple-git-accounts-demo.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment