I have two Github accounts: oanhnn (personal) and superman (for work). I want to use both accounts on same computer (without typing password everytime, when doing git push or pull).
Use ssh keys and define host aliases in ssh config file (each alias for an account).
-
Generate ssh key pairs for accounts and add them to GitHub accounts.
-
Edit/Create ssh config file (
~/.ssh/config
):# Default github account: oanhnn Host github.com HostName github.com IdentityFile ~/.ssh/oanhnn_private_key IdentitiesOnly yes # Other github account: superman Host github-superman HostName github.com IdentityFile ~/.ssh/superman_private_key IdentitiesOnly yes
NOTE: If you use any account frequently, you should use the default hostname (
github.com
). -
Add ssh private keys to your agent:
$ ssh-add ~/.ssh/oanhnn_private_key $ ssh-add ~/.ssh/superman_private_key
-
Test your connection
$ ssh-keyscan github.com >> ~/.ssh/known_hosts $ ssh -T [email protected] $ ssh -T git@github-superman
If everything is OK, you will see these messages:
Hi oanhnn! You've successfully authenticated, but GitHub does not provide shell access.
Hi superman! You've successfully authenticated, but GitHub does not provide shell access.
-
Now all are set, you need remeber
git@github-superman:org/project.git => user is superman [email protected]:org/project.git. => user is oanhnn
- If you need clone a repository, just do:
$ git clone git@github-superman:org1/project1.git /path/to/project1
$ cd /path/to/project1
$ git config user.email "[email protected]"
$ git config user.name "Super Man"
- If you already have the repo set up, after the ssh config instructions, you need change the URL of
origin
, just do:
$ cd /path/to/project2
$ git remote set-url origin git@github-superman:org2/project2.git
$ git config user.email "[email protected]"
$ git config user.name "Super Man"
- If you are creating a new repository on local:
$ cd /path/to/project3
$ git init
$ git remote add origin git@github-superman:org3/project3.git
$ git config user.email "[email protected]"
$ git config user.name "Super Man"
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master
Done! Goodluck!
The bash script that prompts for your git account. Thank @davorpa
#!/bin/bash
# silent prompt
read -p 'GIT profile: ' profile
# switch
case $profile in
superman)
git config user.email "[email protected]"
git config user.name "superman"
git config user.signingKey "superman_gpg_public_key"
;;
oanhnn)
git config user.email "[email protected]"
git config user.name "oanhnn"
git config user.signingKey "oanhnn_gpg_public_key"
;;
# default case: raise error
*)
>&2 echo "ERR: Unknown profile: $profile"
exit 1
esac
Hi there! Are any of you guys using this setup also using Fork as a git client or GUI for git? I am asking this, because I have some problems with
fetching/pull/pushing
my personal repo.Here's my setup of
.ssh/config
file:Work github account: peter.mihok-work
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_work
IdentitiesOnly yes
UseKeychain yes
AddKeysToAgent yes
ControlMaster no
Personal github account: mihokpeter
Host github.com-mihokpeter
HostName github.com
IdentityFile ~/.ssh/id_mihokpeter
IdentitiesOnly yes
UseKeychain yes
AddKeysToAgent yes
ControlMaster no
What I don't understand is that even-thought my remote origin url is set either
[email protected]:something/file.git
or[email protected]:mihokpeter/file.git
. In my personal repo I also addedgit config user.email
andgit config user.name
. While using Fork I can easily push/pull/etc.. on my work branch, but I can't do none of this on my personal one. However, I can do all of those in terminal in my personal branch. So, with that said I think my ssh must be setup correctly (as those git commands are working) + I can see correct names being printed out in terminal after runningssh -T [email protected]
andssh -T [email protected]
.Any ideas what can be wrong? I am really struggling here and I would appreciate any help!