Multiple github accounts on the same computer?

2018-12-31 16:16发布

Trying to work on my both my actual "work" repos, and my personal repos on git hub, from my computer.

The work account was set up first, and everything works flawlessly.

My personal account, however cannot seem to push to my personal repo, which is set up under a different account/email.

I've tried copying my work key up to my personal account, but that throws an error, because of course a key can be only attached to one account.

How can I push/pull to and from both accounts, from their respective github credentials?

15条回答
余欢
2楼-- · 2018-12-31 16:51
  • Go to ~/.ssh
  • Create a file named config(have no extension )
  • Open config file & add below codes. (change according to your account)

    1. Account 1

      # account_1
      Host gitlab.com-account_1
      HostName gitlab.com
      User git
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/id_rsa_account_1
      
    2. Account 2

      # Account2
      Host gitlab.com-Account2
      HostName gitlab.com
      User git
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/id_rsa_Account2
      
    3. Account 3

      # Account_3
      Host github.com-Account3
      HostName github.com
      User git
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/id_rsa_Account_3
      
  • Add remote url as follows

    1. Account 1

      git remote add origin git@gitlab.com-account_1:group_name/repo_name.git
      
    2. Account 2

      git remote add origin git@gitlab.com-Account2:group_name/repo_name.git
      
    3. Account 3

      git remote add origin github.com-Account3:github_username/repo_name.git
      

Make sure that IdentityFile names are same as you created during ssh key generation.

查看更多
明月照影归
3楼-- · 2018-12-31 16:56

Beside of creating multiple SSH Keys for multiple accounts you can also consider to add collaborators on each project using the same account emails and store the password permanently.

#this store the password permanently
$ git config --global credential.helper wincred

I have setup multiple accounts with different emails then put the same user and email on each account as one of the collaborators. By this way I can access to all account without adding SSH Key, or switching to another username, and email for the authentication.

查看更多
浪荡孟婆
4楼-- · 2018-12-31 16:56

Use HTTPS:

change remote url to https:

git remote set-url origin https://USERNAME@github.com/USERNAME/PROJECTNAME.git

and you are good to go:

git push

To ensure that the commits appear as performed by USERNAME, one can setup the user.name and user.email for this project, too:

git config user.name USERNAME
git config user.email USERNAME@example.com
查看更多
登录 后发表回答