Let say you have 2 (or more) github accounts: ghaccusr1 and ghaccusr2. You want to use both of them on the same client (computer). What you need are:
- Multiple SSH keys (id_rsa_*) and add the SSH key to the appropriate account on Github.
- Add and configure your ~/.ssh/config.
- Sync remote repo to your local project folder.
Step 1: Generate SSH keys and add the generated keys to the Github accounts.
- Generate SSH key for: ghaccusr1
- $ touch ~/.ssh/id_rsa_ghaccusr1
- $ touch ~/.ssh/id_rsa_ghaccusr1.pub
- $ ssh-keygen -t rsa -C "your-email-address-for-ghaccusr1"
- Enter a file in which to save the key (/Users/{you}/.ssh/id_rsa): id_rsa_ghaccusr1
- Enter passphrase (empty for no passphrase): [Type a passphrase]
- Enter same passphrase again: [Type passphrase again]
- $ eval "$(ssh-agent -s)"
- $ ssh-add ~/.ssh/id_rsa_ghaccusr1
- $ pbcopy < ~/.ssh/id_rsa_ghaccusr1.pub
- Go to github.com > login to: ghaccusr1 > Settings > SSH Keys > New SSH bey
- Type in any name in the "Title" text field.
- Press "Ctrl + V" or "Command + V" in the "Key" text field.
- Click "Add SSH key"
- Generate SSH key for: ghaccusr2
- $ touch ~/.ssh/id_rsa_ghaccusr2
- $ touch ~/.ssh/id_rsa_ghaccusr2.pub
- $ ssh-keygen -t rsa -C "your-email-address-for-ghaccusr2"
- Enter a file in which to save the key (/Users/{you}/.ssh/id_rsa): id_rsa_ghaccusr2
- Enter passphrase (empty for no passphrase): [Type a passphrase]
- Enter same passphrase again: [Type passphrase again]
- $ eval "$(ssh-agent -s)"
- $ ssh-add ~/.ssh/id_rsa_ghaccusr2
- $ pbcopy < ~/.ssh/id_rsa_ghaccusr2.pub
- Go to github.com > login to: ghaccusr1 > Settings > SSH Keys > New SSH bey
- Type in any name in the "Title" text field.
- Press "Ctrl + V" or "Command + V" in the "Key" text field.
- Click "Add SSH key"
Step 2: Create / edit ~/.ssh/config file.
- If the config file doesn't exist yet: $ touch ~/.ssh/config. Otherwise, proceed to 2.
- vim ~/.ssh/config
#ghaccusr1 GitHub
Host github-ghaccusr1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_ghaccusr1
#ghaccusr2 Github
Host github-ghaccusr2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_ghaccusr2
- Press "Esc" key.
- Type in: ":wq!"
Step 3: Test connecting to your Github repo.
- Test for "ghaccusr1"
- cd /to/the/path/of/your/project/folder
- git init
- git remote add origin [email protected]github-ghaccusr1:ghaccusr1/[repo-name].git (assuming you already create a repo on Github)
- git pull origin master, or
- git add *
- git commit -m "Some message -- could be my initial commit"
- git push origin master
- Test for "ghaccusr2"
- cd /to/the/path/of/your/project/folder
- git init
- git remote add origin [email protected]github-ghaccusr2:ghaccusr1/[repo-name].git (assuming you already create a repo on Github)
- git pull origin master, or
- git add *
- git commit -m "Some message -- could be my initial commit"
- git push origin master
That is it!