On my company laptop, I want to be able to use my personal github account as well as the company one, and to push commits to repos using the proper account. How could I do that?
Thanks Eric for the tutorial
Scenario:
I have 2 github accounts, one for my own and one for the company.
On the company laptop, I have copied my own github ssh keys. But the new company github account has no ssh key yet.
Let’s denote the 2 accounts as follows:
1 | puppy// personal .com |
Step 1 - Create a New SSH Key for company account
1 | ssh-keygen -t rsa -C "puppy@company.com" |
Be careful that you don’t over-write your existing key for your personal account. Instead, when prompted, save the file as id_rsa_COMPANY
. In my case, I’ve saved the file to ~/.ssh/id_rsa_COMPANY
.
Step 2 - Attach the New Key
Next, login to your second GitHub account, browse to “Account Overview,” and attach the new key, within the “SSH Public Keys” section. To retrieve the value of the key that you just created, return to the Terminal, and type: cat ~/.ssh/id_rsa_COMPANY.pub
. Copy the entire string that is displayed, and paste this into the GitHub textarea. Feel free to give it any title you wish
Next, because we saved our key with a unique name, we need to tell SSH about it. Within the Terminal, type: ssh-add ~/.ssh/id_rsa_COMPANY
. If successful, you’ll see a response of “Identity Added.”
Step 3 - Create a Config File
Now, it’s time to specify when we wish to push to our personal account, and when we should instead push to our company account. To do so, let’s create a config file.
open ~/.ssh/config
, if it doesn’t exist, create it by touch ~/.ssh/config
Now, add the following to the config file:
1 | #Default GitHub |
Step 4 - Try it Out
To push the repo to your own github account:
1 | git remote add origin git@github:yingchi/fastai-notes.git |
To push the repo to company github account:
1 | git remote add origin git@github-COMPANY:COMPANY/testing.git |
Clone from company private repo:
1 | git clone git@github-COMPANY:COMPANY/lalala.git |
Setting your github account for a single repository
1 | $ git config user.email "email@example.com" |
Confirm that you have set the email address correctly in Git:
1 | $ git config user.email |