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
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/
Open config file & add below codes. (change according to your account)
Account 1
# account_1
Host gitlab.com-account_1
HostName gitlab.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_account_1
Account 2
# Account2
Host gitlab.com-Account2
HostName gitlab.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_Account2
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
Account 1
git remote add origin git@gitlab.com-account_1:group_name/repo_name.git
Account 2
git remote add origin git@gitlab.com-Account2:group_name/repo_name.git
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.
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.
IntelliJ Idea has built-in support of that https://www.jetbrains.com/help/idea/github.html#da8d32ae
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):
ls -al ~/.ssh
.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.ssh-keygen -t rsa -C "the copied email"
, you'll see:/.../.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.ls -al ~/.ssh
again.pbcopy < ~/.ssh/id_rsa_work.pub
, replace id_rsa_work.pub
with what you called yours.id_rsa_work
] and navigate toEdit 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.
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:
Create a new SSH key for the second account with ssh-keygen -t rsa -f ~/.ssh -f ~/.ssh/custom_id_rsa
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
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