6

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 = 12345+doug@users.noreply.github.com
    name = 12345doug

[includeIf "gitdir:/home/doug/Documents/Projects/Zen/"]
    path = ~/.git-zen

/.git-zen looks like this:

[user]
        email = doug@work.com
        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
doug@work.com

To create my ssh keys I followed this post on github.com.

ssh-keygen -t rsa -b 4096 -C "doug@work.com"
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 git@github.com: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?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • I'm having the same problem; `ERROR: Repository not found.` is triggered because the repository is private. I don't know why using an alternate SSH identity causes a problem here, or how to fix it. – Barton Chittenden Oct 20 '21 at 14:58
  • 1
    Check out https://stackoverflow.com/a/61150726/266747; there are some strategies for associating `user.email` with a given ssh IdentityFile. I would also suggest adding `IdentitiesOnly=yes` for a given `Host` entry in `~/.ssh/config`, this will keep ssh from trying other identities. – Barton Chittenden Oct 20 '21 at 17:38

3 Answers3

10

First, a bit of background: in the URL you use here:

git clone git@github.com:...

the only really significant part for this problem is the git@github.com part. You can also spell this:

git clone ssh://git@github.com/...

(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 git@github.com

Had your URL said git@corp.com you'd want to run ssh -T git@corp.com 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 git@github.com 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 git@github.com

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?).

torek
  • 448,244
  • 59
  • 642
  • 775
  • In `.ssh/config` you have `as-m` and `as-q`. Now I'm in my repo with commits to push. How can I tell git which config to use? As opposed to just `git push` do I need to add e.g. `git push as-m`? – Doug Fir Oct 21 '21 at 12:57
  • When I run `ssh -T git@github.com` from within the work repo, I get "Hi 12345doug! You've successfully authenticated, but GitHub does not provide shell access." it looks like it's using my personal git credentials and not my work one `doug-work`. Perhaps my includeif is off? – Doug Fir Oct 21 '21 at 12:58
  • Some more info. From within the work repo when I go `git config user.name` I do see the correct work user name `doug-work` and not personal `12345doug` – Doug Fir Oct 21 '21 at 13:02
  • Finally got this working. Had to update the origin and replace `git@github.com` with the desired config e.g. `as-m` or `as-q` based on config examples above. Full example, suppose the origin is `git@github.com:ourcompany/myrepo.git` then change with `git remote set-url origin as-m:ourcompany/myrepo.git` – Doug Fir Oct 21 '21 at 15:38
  • 1
    Note that `user.name` and `user.email` are *not used during authentication*. They are used much earlier, when you make new commits. It's the *host name* in the ssh URL that makes ssh look at the `.ssh/config`, and it's the `.ssh/config` that controls which key(s) ssh will send. – torek Oct 21 '21 at 15:47
1

Try this if you are still facing issue even after adding SSH key.

git remote -v

This will show you the remote SSH URL then,

git remote set-url origin "SSHURL"

Paste the URL in the above line where SSHURL is.

1

In my Mac I was facing the same issue. so here's the way i resolved the same

Step 1:

create a new ssh key:-

ssh-keygen -t ed25519 -C "your_email@example.com"

Step 2:

copy the ssh key on clipboard :-

pbcopy < ~/.ssh/id_ed25519.pub 

Step 3:

now paste copied ssh key to the corresponding git repository

Step 4: Start the ssh-agent in the background.

$ eval "$(ssh-agent -s)"
> Agent pid 59566

Step 5: now try accesing the repo

git clone git@github.com:username/repo-name.git

Step 6:

if still you see the issue then check the ssh config file

vim ~/.ssh/config

the content should look like

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

Step 7:

Add your SSH private key to the ssh-agent and store your passphrase in the keychain

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Step 8:

Now try again it should work

git clone git@github.com:username/repo-name.git
officialrahulmandal
  • 2,473
  • 1
  • 23
  • 31