Setting Remote for Multiple GitHub Accounts on Multiple Devices

less than 1 minute read

Published:

  1. If you install Xcode, need to turn off credential.helper.
    Credential-osxkeychain will automatically deal with your key.  
    We want to do it manually so we can have more than one key.

     git config --show-origin --get credential.helper  
     file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig osxkeychain   
     vim /Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig  
    
  2. Go to Keychain Access and remove all key in github.com.
  3. Generate ssh key in your .ssh. Suppose we name them id_rsa_kenway and id_rsa_cw.
     ssh-keygen -t rsa -C "your-email-address"  
    
  4. Go to your GitHub page setting -> SSH and GPG keys -> New SSH key, paste the public key.
  5. (Optional) Attach the new key.
     (delete all attached key)  
     ssh-add -D  
          
     (list all attached key)  
     ssh-add -l  
     ssh-add ~/.ssh/id_rsa_aaa  
     ssh-add ~/.ssh/id_rsa_bbb  
    
  6. Touch file name “config” with the following code snippet.
     Host github.com-aaa  
             HostName github.com  
             User git  
             IdentityFile ~/.ssh/id_rsa_aaa  
     Host github.com-bbb  
             HostName github.com  
             User git  
             IdentityFile ~/.ssh/id_rsa_bbb  
    
  7. Set remote node on local folder.
     git config user.name "bbb"  
     git config user.email "bbb@outlook.com"  
     git remote set-url origin git@github.com-aaa:aaa/testkey.git  
     git remote set-url origin git@github.com-bbb:bbb/testkey2.git  
    
  8. Check your GitHub remote.
     git remote -v