1

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.

Alex Bollbach
  • 4,370
  • 9
  • 32
  • 80

1 Answers1

3

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • can you explain "workgh:myWorkLogin" a bit more? and will this configuration allow me to robustly communicate with arbitrary branches on repos from either of my github accounts / organizations? do I need to take special care to configure the remote each time I synchronize with any remote repo in either of my contexts (work/personal)? – Alex Bollbach Jul 21 '17 at 14:08
  • 1
    @AlexBollbach Sorry, I was away. The important part is `workgh:` that is the key which will be replaced by git@github.com, *but* will use the right ssh key. The rest of the url (like `myWorkLogin/myWorkrepo.git`) is the normal GitHub URL that you would use after `git@github.com` – VonC Jul 21 '17 at 15:56
  • thanks, after some hitches this appears to have worked. perhaps this nightmare with git auth has finally ended and i can actually write some code! btw, using this approach, must I take care to construct remote-repo url's with this schema? usually i would just do `git remote add origin `. seems like from now own remote url's need this config aware Host prepended to the usual git ssh-url, correct? – Alex Bollbach Jul 21 '17 at 16:08
  • 1
    @AlexBollbach Yes, that is the idea: you add (or replace with set-url as in my answer) the classic `git@github.com:myuser/myrepo` with `mykey:myuser/myrepo`, and `mykey` is an entry in your `~/.ssh/config` which will tell ssh to use `git@github.com`, but more importantly, will tell `ssh` to use the right ssh key. – VonC Jul 21 '17 at 16:12