Multiple github accounts on the same computer?

后端 未结 25 2273
野的像风
野的像风 2020-11-22 02:56

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 flawl

相关标签:
25条回答
  • 2020-11-22 03:42

    You do not have to maintain two different accounts for personal and work. In fact, Github Recommends you maintain a single account and helps you merge both.

    Follow the below link to merge if you decide there is no need to maintain multiple accounts.

    https://help.github.com/articles/merging-multiple-user-accounts/

    0 讨论(0)
  • 2020-11-22 03:44
    • 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.

    0 讨论(0)
  • 2020-11-22 03:44

    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.

    0 讨论(0)
  • 2020-11-22 03:44

    IntelliJ Idea has built-in support of that https://www.jetbrains.com/help/idea/github.html#da8d32ae

    0 讨论(0)
  • 2020-11-22 03:46

    This answer is for beginners (none-git gurus). I recently had this problem and maybe its just me but most of the answers seemed to require rather advance understanding of git. After reading several stack overflow answers including this thread, here are the steps I needed to take in order to easily switch between GitHub accounts (e.g. assume two GitHub accounts, github.com/personal and gitHub.com/work):

    1. Check for existing ssh keys: Open Terminal and run this command to see/list existing ssh keys ls -al ~/.ssh
      files with extension .pub are your ssh keys so you should have two for the personal and work accounts. If there is only one or none, its time to generate other wise skip this.

      - Generating ssh key: login to github (either the personal or work acc.), navigate to Settings and copy the associated email.
      now go back to Terminal and run ssh-keygen -t rsa -C "the copied email", you'll see:

      Generating public/private rsa key pair.
      Enter file in which to save the key (/.../.ssh/id_rsa):


      id_rsa is the default name for the soon to be generated ssh key so copy the path and rename the default, e.g. /.../.ssh/id_rsa_work if generating for work account. provide a password or just enter to ignore and, you'll read something like The key's randomart image is: and the image. done.
      Repeat this step once more for your second github account. Make sure you use the right email address and a different ssh key name (e.g. id_rsa_personal) to avoid overwriting.
      At this stage, you should see two ssh keys when running ls -al ~/.ssh again.
    2. Associate ssh key with gitHub account: Next step is to copy one of the ssh keys, run this but replacing your own ssh key name: pbcopy < ~/.ssh/id_rsa_work.pub, replace id_rsa_work.pub with what you called yours.
      Now that our ssh key is copied to clipboard, go back to github account [Make sure you're logged in to work account if the ssh key you copied is id_rsa_work] and navigate to
      Settings - SSH and GPG Keys and click on New SSH key button (not New GPG key btw :D)
      give some title for this key, paste the key and click on Add SSH key. You've now either successfully added the ssh key or noticed it has been there all along which is fine (or you got an error because you selected New GPG key instead of New SSH key :D).
    3. Associate ssh key with gitHub account: Repeat the above step for your second account.
    4. Edit the global git configuration: Last step is to make sure the global configuration file is aware of all github accounts (so to say).
      Run git config --global --edit to edit this global file, if this opens vim and you don't know how to use it, press i to enter Insert mode, edit the file as below, and press esc followed by :wq to exit insert mode:

      [inside this square brackets give a name to the followed acc.] name = github_username email = github_emailaddress [any other name] name = github_username email = github_email [credential] helper = osxkeychain useHttpPath = true

    Done!, now when trying to push or pull from a repo, you'll be asked which GitHub account should be linked with this repo and its asked only once, the local configuration will remember this link and not the global configuration so you can work on different repos that are linked with different accounts without having to edit global configuration each time.

    0 讨论(0)
  • 2020-11-22 03:46

    In case you don't want to mess with the ~/.ssh/config file mentioned here, you could instead run git config core.sshCommand "ssh -i ~/.ssh/custom_id_rsa" in the repo where you want to commit from a different account.

    The rest of the setup is the same:

    1. Create a new SSH key for the second account with ssh-keygen -t rsa -f ~/.ssh -f ~/.ssh/custom_id_rsa

    2. Sign in to github with your other account, go to https://github.com/settings/keys , and paste the contents of ~/.ssh/custom_id_rsa.pub

    3. Make sure you're using SSH instead of HTTPS as remote url: git remote set-url origin git@github.com:upstream_project_teamname/upstream_project.git

    0 讨论(0)
提交回复
热议问题