Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

git - What steps must I take to consistently push to two Github accounts using SSH?

I commit to Github for work and for personal use. For the last two months I have consistently had trouble each and every time I switch github contexts. Every night (and every morning) I run into error's pushing to or pulling form Github like this:

ERROR: Permission to AlexanderBollbach/test.git denied to alexbollbach.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

If I can get the pushing/pulling to work properly (usually through some circuitous path of reading StackOverflow posts about SSH/Github/Git and not understanding them in any depth), I immediately run into the same problems again when I switch from work Github to home or vice versa.

The only interesting clue I have noticed is that in denied to alexbollbach, "alexbollbach" is my work Github account username. hmm, this leads me to consider ~/.ssh/config, whose contents are:

Host *
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa

Host github.com
 User git
Host helper
    HostName <work-domain-stuff>
    IdentityFile ~/.ssh/eng_rsa
    User eng

I was told that this file is instrumental in configuring git's ssh attempts. But I still cannot make the connection between this file and the git push error thinking that I am alexbollbach. In fact, I do not know how ssh, my key pairs, and the config file relate to the notion of Github user's in any sense.

Please help. Preferably not just a fix but some clarification on what i'm not grasping. I routinely spend an hour at least twice a week running into this issue.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The way you link a remote repo and different ssh keys in Git is through the remote url.
Change those urls like so:

 cd /path/to/my/work/repo
 git remote set-url origin workgh:myWorkLogin/myWorkrepo.git

 cd /path/to/my/perso/repo
 git remote set-url origin persgh:myPersoLogin/myPersorepo.git

See "How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?" as a full example.
Your ~/.ssh/config will reference the right ssh key for each url:

# Personal GitHub
Host persgh
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_perso

# Work GitHub
Host workgh
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_work

Don't use "github.com" as a "Host": using an special name for the Host key is more explicit and indicates this is an ssh url you have to resolve through a ~/.ssh/config file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...