Issue
Yesterday I created a ssh key and cloned a repo for work. After making some changes I attempted to push a commit:
git push
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I have read this active SO post. My issue persists. I don't know how to track down the issue so will share everything that I think is relevant.
On my laptop I have both personal and work repos. My work work lives in any directory nested under ~/Documents/Projects/Zen/Projects/
. Anywhere else e.g. ~/Documents/Projects/Personal/
is for personal github.
My .gitconfig looks like this:
[user]
email = [email protected]
name = 12345doug
[includeIf "gitdir:/home/doug/Documents/Projects/Zen/"]
path = ~/.git-zen
/.git-zen looks like this:
[user]
email = [email protected]
name = doug-work
Currently I'm in a repo:
pwd
/home/doug/Documents/Projects/Zen/Projects/analytics-psql-action
With some commits ready to push:
~/Documents/Projects/Zen/Projects/analytics-psql-action$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
My git config in this dir seems correct:
git config user.name
doug-work
and
git config user.email
[email protected]
To create my ssh keys I followed this post on github.com.
ssh-keygen -t rsa -b 4096 -C "[email protected]"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
I then added the key per this tutorial to my work github settings.
So far I think I have everything set up as it should? But when I try to push from within this repo:
~/Documents/Projects/Zen/Projects/analytics-psql-action$ git push
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
In desperation I renamed the repo and attempted to clone from fresh:
git clone [email protected]:work/analytics-psql-action.git # real path actually used not 'work'
Cloning into 'analytics-psql-action'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
On the repo itself, if I go into repo settings > manage access I can see myself, doug-work has admin access rights.
How can I clone and push to the repo? Where should I start in trying to find out why my set up of ssh keys are not working?
Solution
First, a bit of background: in the URL you use here:
git clone [email protected]:...
the only really significant part for this problem is the [email protected]
part. You can also spell this:
git clone ssh://[email protected]/...
(using exactly the same rest-of-text in place of ...
) with the exact same effect.
Next, to diagnose the issue, you want to run ssh
from the command line:
ssh -T [email protected]
Had your URL said [email protected]
you'd want to run ssh -T [email protected]
here, and so on. That is, we take the user@host
part and run ssh -T user@host
. This makes this work for various company-hosted offshoot variants, not just GitHub. It also lets us use more than one login on GitHub: see more below.
Now, having run ssh -T [email protected]
you could get a permission denied error, meaning that none of the keys you tried over at github.com worked, or you could get this kind of output:
Hi M! You've successfully authenticated, but but GitHub does not provide shell access.
(Here, we've authenticated ourselves to GitHub as M, head of the James Bond spy division.)
Now, if M has permission to access (read and write) the repositories, we'll be able to read and write the repositories. But maybe M only has read access, and it's actually Q who has write access. We stole both M's and Q's keys, but how do we get in as Q?
Before we get to that point, let's see what to do if this kind of authentication fails entirely. Our next step is to run:
ssh -Tv [email protected]
This produces a lot output, all prefixed with debug1
. There may be some clues here as to what has gone wrong. If not, we can use ssh -Tvv
, or ssh -Tvvv
, to get even more debug output, prefixed with debug2
and debug3
. The usual suspects here when looking through all this output are to make sure that ssh is looking at the correct .ssh/*
files, in which your ssh keys are stored. Make sure that:
- ssh has the right path;
- ssh tries the right files therein; and
- it can actually read these files.
If not, look for your OS-specific methods for giving ssh the right permissions and/or paths.
Using multiple identities
As the evil spy breaking into the MI-5 records, we have compromised the secret keys of both M and Q, and we now wish to insert our Trojan horse into the system so that we can attempt an overly elaborate death for James Bond. But that's for later: for now, we're getting in as M, but we need to get in as Q. What shall we do?
In order to get in to GitHub as Q instead of M, we need to configure ssh correctly. The reason is simple: just listing all our keys in our .ssh
directory doesn't work. ssh tries them in some order, and clearly, our ssh is trying our M key first. As soon as that works, it's in, and ssh stops trying further keys.
We need to have our ssh try our Q key first. We could remove our key for M, but that is annoying: now we can plant our Trojan horse, but we can't find Bond's current assignment, which is only readable by M. What we'd like to have is a way to pick which key we'll try.
This is where the .ssh/config
file comes in:
Host as-q
Hostname github.com
User git
IdentitiesOnly yes
IdentityFile ~/.ssh/stolen-q-key
Host as-m
Hostname github.com
User git
IdentitiesOnly yes
IdentityFile ~/.ssh/stolen-m-key
Now we can list our two repositories as ssh://as-m/mi5/bond-assignments.git
and ssh://as-q/mi5/spy-devices.git
, or as-m:mi5/bond-assignments.git
and as-q:mi5/spy-devices.git
. (That is, once you get this part working, you'll need to update the URLs associated with your clones. Use git remote set-url origin new-url
or git config --edit
or whatever your favorite way is to achieve this.)
When we ssh as-q
, we'll have ssh connect to github.com (Hostname
), use git
as the user name, and use the stolen Q key as the key. When we ssh as-m
, we'll connect to the same host and use the same user, but use the stolen M key this time.
I did all that and ssh works but Git-for-Windows still doesn't
You've run into one other problem:
- Old versions of Windows came with no, or an inadequate, ssh.
- So Git-for-Windows bundles an ssh (in the same way that it bundles a version of bash: git-bash isn't really part of Git, it's just needed to use Git, and the bundled ssh isn't part of Git either, it's just need to use Git).
- But the bundled ssh and the system ssh are different versions that use different config files.
If the system ssh works fine, tell Git to use the system ssh, e.g., git config --global core.sshCommand ssh
. If it's inadequate, use the bundled ssh (but then how did your testing above work?).
Answered By - torek Answer Checked By - Mary Flores (WPSolving Volunteer)