Detailed steps to use two GitHub accounts on the same PC as below:
1. Create SSH key for the github.company.com account
If you already added the SSH key to your github.company.com account, then skip this step.
First, use ssh-keygen
to create id_rsa
and id_rsa.pub
in C:Usersusername.ssh
.
Then, add the content of id_rsa.pub file
as a SSH key in github.company.com account.
2. Create SSH Key for your personal account github.com
Use ssh-keygen -t rsa -C "email address for the personal github account"
, and save the key in /c/Users/username/.ssh/id_rsa_personal
.
Now add the content of id_rsa_personal.pub
file as a SSH Key in your personal GitHub account.
3. Config the two SSH Keys
In C:Usersusername.ssh
diectory, create a config
file with below content:
Host github.company.com
HostName github.company.com
User git
IdentityFile ~/.ssh/id_rsa
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
4. Work with the two GitHub accounts by SSH protocol
You can clone your github.company.com account by:
git clone [email protected]:companyaccountname/reponame
And then add the personal account as a remote in the local repo by:
git remote add personal git@github-personal:personalaccountname/reponame
To get the file from personal account to company repo by below commands:
git merge upstream master --allow-unrelated-histories
# make changes
git add .
git commit -m 'add the changes from peronal repo'
git push origin master
To commit/push changes from local company repo by the committer of the personal repo, you just need to re-config the username and email before committing changes in the local repo:
git config user.name <personal github account username>
git config user.email <email for login the personal github account>
When you want to commit changes with the company account, just re-config username and email again before committing changes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…