copy pasted from websites , copyright is not mine.
Generate a second key
Let’s say you already have an ssh key added to your personal github profile and now you want to add one to a work github account. First you may want to see how many ssh keys you have:
$ cd ~/.ssh
$ ls
id_rsa id_rsa.pub
Currently you only have one public key. To generate a second one run:
$ ssh-keygen -t rsa -C "your_email@example.com"
This will create a new ssh key with the supplied email as a label. When prompted for a file enter a new file name, like ‘idrsasc’, otherwise it will try to override the already existing id_rsa key. When prompted for a passphrase, press enter (or choose a passphrase) and confirm. You know you’ve succeeded when you see something like this:
Your public key has been saved in /Users/jasminaatanassova/.ssh/id_rsa_sc.pub
Add identities to the keys.
加入剛才生成的rsa key的private key
$ ssh-add ~/.ssh/yourkey.rsa
檢查是否有成功加入
$ssh-add -l
Add ssh key to GitHub
Copy the second key you created to 剪貼簿 (可以用其他方式 指令不一定適用):
$ pbcopy < ~/.ssh/id_rsa_sc.pub
To add it to your work github account go to Account settings > SSH Keys. Click ‘Add SSH key’ and paste the key.
In the terminal cd into your work directory and run git pull to see if the key works. You should see an error like this:
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Add a config file
You need to add a config file which will tell github which shh key to use for which account. Add a config file in your .ssh directory with the following details:
#personal account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
#work account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_sc
Ensure that at least one account has a custom Host, in this example it’s the work one. Otherwise github defaults to the first one and as a result you may get the same access error from above.
接下來的超重要 是從另一篇剪過來的 是使用github的關鍵 因為github是同網址 會抓不到 要用這種方式去寫新的特殊的網址 (自訂字樣會被取代)
~/.ssh/config
Host github-kc
Hostname github.com
User git
IdentityFile ~/.ssh/github_rsa_kc.pub
# LogLevel DEBUG3
Host github-abc
Hostname github.com
User git
IdentityFile ~/.ssh/github_rsa_abc.pub
# LogLevel DEBUG3
Set remote url for repo:
- For repo in Host
github-kc
:
git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git
- For repo in Host
github-abc
:
git remote set-url origin git@github-abc:abcdefg/yyy.git