search by tags

for the user

adventures into the land of the command line

how to git pull from two remote private repo's on the same computer

i had this problem where i had two computers in amazon, and both of them were pulling from git from a unique git repo. then, cos im cheap, i moved both the apps onto the same computer. that meant both apps would be trying to connect to github using two different key pairs. so i had this problem, how do you get git to differentiate between which key pair to use, for a particular repo, when there is more than one on the same computer?

git says you can do it like this

the problem can show itself like this

$ git pull
ERROR: Repository not found.
fatal: The remote end hung up unexpectedly

it’ll be confusing because the repos exist

$ sudo ls -al /var/www/repo1/
total 240
drwxrwxr-x 3 deploy deploy  4096 Mar  4 11:42 .
drwxrwxr-x 4 deploy deploy  4096 Feb 25 21:27 ..
-rw-r--r-- 1 deploy deploy  1399 Feb 29 23:31 index.py
-rw-r--r-- 1 deploy deploy   145 Feb 25 21:25 myapp.wsgi
drwxr-xr-x 8 deploy deploy  4096 Mar  4 11:40 .git
-rw-r--r-- 1 deploy deploy    52 Feb 25 21:25 .gitignore
-rw-rw-r-- 1 deploy deploy 99497 Mar  4 11:05 somemorestuff.py

$ sudo ls -al /var/www/repo2/
total 240
drwxrwxr-x 3 deploy deploy  4096 Mar  4 11:42 .
drwxrwxr-x 4 deploy deploy  4096 Feb 25 21:27 ..
-rw-r--r-- 1 deploy deploy  1399 Feb 29 23:31 index.py
-rw-r--r-- 1 deploy deploy   145 Feb 25 21:25 myapp.wsgi
drwxr-xr-x 8 deploy deploy  4096 Mar  4 11:40 .git
-rw-r--r-- 1 deploy deploy    52 Feb 25 21:25 .gitignore
-rw-rw-r-- 1 deploy deploy 99497 Mar  4 11:05 somemorestuff.py

and also you have keys for both repos, even more confusing

$ sudo ls -l /home/deploy/.ssh/
total 28
-rw-------. 1 deploy deploy  410 Jan 21 22:16 authorized_keys
-rw-------  1 deploy deploy  193 Mar  4 11:40 config
-rw-------  1 deploy deploy 1675 Feb 25 21:25 id_rsa_repo1
-rw-------  1 deploy deploy  410 Feb 25 21:25 id_rsa_repo1.pub
-rw-------  1 deploy deploy 1671 Feb 25 21:28 id_rsa_repo2
-rw-------  1 deploy deploy  410 Feb 25 21:28 id_rsa_repo2.pub
-rw-r--r--  1 deploy deploy 1595 Mar  4 11:22 known_hosts

and even MORE confusing… you’ve set up your ssh config file to specify an alias for github.com and which key to use for which repo

$ sudo cat /home/deploy/.ssh/config

host repo1.github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa_repo1
 User deploy

host repo2.github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa_repo2
 User deploy

the problem is, in your git config, the url you’re trying to connect to for both repos is github.com, and so your ssh-agent is going to find the first instance of it in the ssh config file, and use those keys. using the wrong keys for the wrong repo essentially.

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]:user/repo1.git

so you might find, repo1 works and repo2 doesn’t, so what you need to do, is subdomain the git url like how you have it named in you ssh config file like this

$ sudo cat /var/www/repo1/.git/config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]:user/repo1.git
[branch "master"]
    remote = origin
    merge = refs/heads/master

$ sudo cat /var/www/repo2/.git/config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]:user/repo2.git
[branch "master"]
    remote = origin
    merge = refs/heads/master

works?

$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From repo2.github.com:user/repo2
   90f51eb..c76de19  master     -> origin/master
Updating 90f51eb..c76de19
Fast-forward
 index.py |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)

works.