Step 1-create a New SSH Key
We need to generate a unique SSH key for our second GitHub account.
Ssh-keygen-t rsa-c "Your-email-address"
Be careful this 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_nettuts.
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 Ke Ys "section. To retrieve the value of the key is just created, return to the Terminal, and Type:vim ~/.ssh/id_rsa_company.pub. Copy the entire string is displayed, and paste this to the GitHub textarea. Feel free to give it any title you wish.
Next, because we saved our keys 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
We ' ve done the bulk of the workload; But now we need a-specify when we wish to the push to our personal account, and if we should instead push to our comp Any account. To does so, let ' s create a config file.
Touch ~/.ssh/config
Vim Config
If you're not comfortable with Vim, feel free to open it within any editor of your choice. Paste in the following snippet.
#Default GitHub
Host github.com
HostName github.com
User git
Identityfile ~/.ssh/id_rsa
The default setup for pushing to our personal GitHub account. Notice that we ' re able to attach a identity file to the host. Let's add another one for the company account. Directly below the code above, add:
Host Github-company
HostName github.com
User git
Identityfile ~/.ssh/id_rsa_company
This time, rather than setting the host to github.com, we ve named it as Github-company. The difference is, we ' re now attaching the new identity file, that we created Previously:id_rsa_company. Save the page and exit!
Step 4-try it out
It ' s time to see if our efforts were successful. Create a test directory, initialize git, and create your first commit.
Git init
Git commit-am "First commit"
Login to your company account, create a new repository, give it a name of ' Test, ' and then return to the Terminal and push Your git repo to GitHub.
git remote add origin [email protected]:company/testing.git
Git push Origin Master
Note that, this time, rather than pushing to [email protected], we ' re using the custom host, which we create in the
Config file: [email protected]
Return to GitHub, and your should now see your repository. Remember:
When the pushing to your personal account, proceed as is always there.
For your company account, make sure, Git!github-company as the host.
Be sure to refer to the screencast if you need a more visual overview of the steps above!
Linux with multiple git accounts on the same machine