Skip to content

Instantly share code, notes, and snippets.

@nikhilsuvarna
Last active July 7, 2018 09:07
Show Gist options
  • Save nikhilsuvarna/fc5f2950126c290b105274039cb5f5e0 to your computer and use it in GitHub Desktop.
Save nikhilsuvarna/fc5f2950126c290b105274039cb5f5e0 to your computer and use it in GitHub Desktop.
managing multiple git accounts
  1. Generate keys for your github user email id :
ssh-keygen -t rsa -b 4096 -C "nsuvarna@pivotal.io"
  1. When prompted for the file to save the key, create a new file, in this case id_rsa_pivotal
Enter file in which to save the key (/Users/nsuvarna/.ssh/id_rsa):
/Users/nsuvarna/.ssh/id_rsa_pivotal

  1. Add the key to github settings : https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/
 pbcopy < ~/.ssh/id_rsa_pivotal.pub
 
  1. Repeat steps 1-3 for another account
  2. Edit ~/.ssh/config file and add the following :
Host pivotal
 Hostname github.com
 User git
 IdentityFile ~/.ssh/id_rsa_pivotal
 PubkeyAuthentication yes
Host personal
 Hostname github.com
 User git
 IdentityFile ~/.ssh/id_rsa
 PubkeyAuthentication yes

for more details on ssh config file, refer to this article

  1. ssh-add to tell SSH about the key you want to use to authenticate on GitHub. ssh-add adds private key identities (from your ~/.ssh directory) to the authentication agent (ssh-agent), so that the ssh agent can take care of the authentication for you, and you don’t have type in passwords at the terminal
ssh-add ~./ssh/id_rsa_personal
ssh-add ~./ssh/id_rsa_pivotal

List the loaded keys

ssh-add -l
  1. Now you’ve got your keys in place, let’s poke github and see if it recognizes us
ssh -T pivotal
Hi nikhilsuvarna! You've successfully authenticated, but GitHub does not provide shell access.
  1. Cloning from a private repo eg from a private repo in the pivotal-gss github org :
git clone git@pivotal:pivotal-gss/labpass

Note the use of the @pivotal - This maps to the alias pivotal in the ~/.ssh/config file for github user nikhilsuvarna

8.1 IF you have the wrong origin set, then use the following to change the origin :

git remote add origin git@pivotal:nikhilsuvarna/tech-talks.git
fatal: remote origin already exists
git remote set-url origin git@pivotal:nikhilsuvarna/tech-talks.git

8.2 Check remote origin

git remote show origin
  1. Push a commit to a private repo
git remote add origin git@pivotal:pivotal-gss/sample-repo.git
git push -u origin master

note the @pivotal should match the alias in the ssh config file from step 5.

  1. Generate API token :

https://help.github.com/articles/creating-an-access-token-for-command-line-use/

git config --local github.token “036c0ca0283355698fda7e94a60eb4f9172c8054"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment