Multiple GitHub/Bitbucket accounts on one system

From Hidden In Plain Sight
Jump to: navigation, search

Using multiple GitHub/Bitbucket accounts on one system is useful when you have administrator rights to the central repository of a project and don't want to be able to accidentally push to it. Although this page refers to GitHub in all the instructions, they equally apply to other Git hosting services such as Bitbucket, you will just need to change references to github to the relevant service.

Creating a new SSH Key

Creating a new SSH Key
  1. Open bash ("Git Bash" on Windows)
  2. Navigate to your ".ssh" directory. (This is usually located in the "~/.shh" directory.)
  3. Create a new ssh key by running:
    ssh-keygen –t rsa –C "<email address>"
    
    1. When asked to "Enter file in which to save the key" enter "id_rsa_developer"
    2. If you want a password then enter one, otherwise leave it blank
  4. Exit bash

Setting up a GitHub Developer Account

  1. Create a new GitHub account to act as your developer account.
  2. Add the SSH public key from the "~/.ssh/id_rsa_developer.pub" (the entire file contents) to your Developer Accounts SSH Keys.
  3. Fork the project repository into you developer account.

If you are using a private repository you now need to grant your developer account access to the repository.

Configuring SSH to use your two accounts

In your "~/.ssh" directory create a file called "config" and add the following:

#Default GitHub (Developer access)
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_developer

#GitHub administrator (Administrator access)
Host github-administrator
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

Adding Remotes to your Local Git Repository

Now you need to add the project repository and your developer fork repositories as remotes to your local repository. The following steps will give you read and write access to you developer fork and read access to the project repository.

Adding your Developer Fork to your Local Git Repository

  1. Goto github and lookup the SSH URL for the forked repository in your developer account
  2. Add the remote in the usual way keeping the address the same as github specified it.
    (e.g. "git@github.com:developer/Project.git" where "developer" specifies your developer account username)

Adding the Project Repository to your Local Git Repository (without write access)

  1. Goto github and lookup the SSH URL for the project repository
  2. Add the remote in the usual way keeping the address the same as github specified it.
    (e.g. "git@github.com:organisation/Project.git" where "organisation" specifies the account that holds the project)

Adding Remotes with write access to the Projects Repository

If you want to be able to push to the Project Repository then you need to add the projects repository specifying your administrator account. Depending on your work flow this isn't strictly necessary as you can pull request to the project repository and

Adding the Project Repository to your Local Git Repository (with write access)

  1. Goto github and lookup the SSH URL for the project repository
  2. Add the remote in the usual way changing the domain from "github.com" to "github-administrator".
    (e.g. If the github SSH URL is "git@github.com:organisation/Project.git" then use "git@github-administrator:organisation/Project.git" where "organisation" specifies the account that holds the project)