Multiple git account
Did you work on several projects? Sometimes you will also use a seperate git account for each project.
If you have trouble to get access - try this...
This example based on a linux system, but meight be comparable to other systems.
My example situation
- I am using a Business Notebook in my professional and private projects
- As a employee i have to use my company account on github.com
- As a freelancer i am using my private account on github.com
Lets configure the solution of our problem
- create (if not already done) your personal SSH Key like discribed on github.com
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" # Creates a new ssh key, using the provided email as a label Generating public/private rsa key pair.
- set "your_email@example.com" to your login mail
- next - save the key
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
- on Linux it's ~/.ssh/ Directory and now » ~/.ssh/git_my_private_key
- If not already done - do it twice with your business mail » ~/.ssh/git_my_business_key
Now we need to configure your standard SSH Config:
~$: nano ~/ssh/config
- you may also use vi
- add this block on your configuration
# Git private Account Host git-private HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/git_my_private_key
Do another block with your business account
# Git business Account Host git-business HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/git_my_business_key
Get ready ?? Let's keep magic happen...
Save your key for SSH access in your account.
The private key at your private account.
The business key at your business account.
Login into your account, and click on "Settings".
Now, click on "New SSH key".
The name of your key is just for identification of yourself. In my case it's "HQNB14" - my business Notebook.
After you saved the key you can start managing your project.
Project settings
If you are starting with a new Project you have to clone your copy - normaly like this:
- clone git@github.com:YOURGITHUBNAME/oc-server3.git
Now chance this to:
- clone git@git-private:YOURGITHUBNAME/oc-server3.git
What happens?
Git ask you maybe for your SSH Key password. Thats all! You should now have access to your GitHub Repro.
In this case, you switched from github.com to a local HOST. This is definied in your SSH config.
This config routes your request to github.com - BUT it knows, it have to use your specified key file.
Did you alreasy cloned and need to reconfigure your settings?
Simple. In your Project directory is a .git/config file.
It contains all settings.
Change your remote origin (your own Fork):
[remote "origin"] url = git@git-private:YOURGITHUBNAME/oc-server3.git fetch = +refs/heads/*:refs/remotes/origin/*
So, set url to "@git-private" and your SSH config will work.
Don't forget to set your personal Info in your Project.
[user] name = YOURGITHUBNAME email = youremail@github
And guess what? Yes! It is the same way to all other Projects. Set it to private or business.
Now - have fun!