6

I want to push my commits to github with ssh. I get

You've successfully authenticated

message after run ssh -T git@github.com command but when I want to push my commits I get this error:

Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

I run ssh-add -l command and I get

4096 SHA256:SREQ0/2G0mT+UxUmpLWmkMramBcFxnl+onFyXgwFENk ~/.ssh/work2_rsa (RSA) 4096 SHA256:gKPZ1Hxzc6eZ/NsgnoPaJsGbdWgQV54bYAXaTym3PfY ~/.ssh/work3_rsa (RSA)

I run git remote -v and I get

origin  git@github.com:H-Ghadirian/UdacitySillySong.git (fetch)
origin  git@github.com:H-Ghadirian/UdacitySillySong.git (push)
sillySong   git@github.com:H-Ghadirian/UdacitySillySong.git (fetch)
sillySong   git@github.com:H-Ghadirian/UdacitySillySong.git (push)

I read this post. Its very similar to my issue but sudo didn't solve my problem.

I also run git push -u origin master and get the same error :

Permission denied (publickey).

I also read Permission denied (publickey). fatal: The remote end hung up unexpectedly for git pull but my public key is in .ssh folder and thats not my case

I read this page: Error: Permission denied (publickey) and check all. Did I miss something?

What should I do?

MacOS: Sierra

git version 2.13.5 (Apple Git-94)

As this link describe,I add config file to my .ssh folder and add

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

to it.

Hamed Ghadirian
  • 6,159
  • 7
  • 48
  • 67
  • 1
    what happens when you run `ssh-add -l`? – Michael Millar Sep 23 '17 at 19:07
  • 1
    Did you make sure that you have the correct access rights and that the repository exists? – Edward Thomson Sep 23 '17 at 19:07
  • @Buccleuch after `ssh-add -l` I get **4096 SHA256:SREQ0/2G0mT+UxUmpLWmkMramBcFxnl+onFyXgwFENk ~/.ssh/work2_rsa (RSA) 4096 SHA256:gKPZ1Hxzc6eZ/NsgnoPaJsGbdWgQV54bYAXaTym3PfY ~/.ssh/work3_rsa (RSA)** – Hamed Ghadirian Sep 23 '17 at 19:11
  • @EdwardThomson what do you mean? I run commands with `sudo` and check repo multiple times. – Hamed Ghadirian Sep 23 '17 at 19:13
  • 1
    you run git as root (`sudo`)? I think you're doing things wrong, this should not be necessary. In any case, check if git uses *that* SSH that you use when you do the `ssh -T`. Looks like git does not have the same SSH configuration. Check the `GIT_SSH` environment variable for example and related config settings. – hakre Sep 24 '17 at 13:28
  • Are you sure you added your public key to GitHub profile? – yorammi Sep 24 '17 at 14:05

2 Answers2

3

Check first what git remote -v does return:

  • it needs to be a repo you own or you are a collaborator of
  • it needs to be the right ssh url git@github.com:<you>/<yourRepo.git>
    (And not an https one like in your question yesterday)

Just to be clear, you need to create the remote repo first on GitHub: pushing to a non-existing repo would generate that error.

Typically, an ssh url not working means said URL is not written the way ssh expects it.
And ssh might expect a different URL because of an ssh config file in ~/.ssh/config.

I run commands with sudo and check repo multiple times.

You did not run your ssh git@github.com with sudi, which means you also don't need sudo for your push commands.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Double check your git config core.sshCommand is as intended.

I also ended up in this situation by copying my .gitconfig from C:\Users\<User>\.gitconfig to ~/.gitconfig in WSL, without realizing however that that copied along the path to an ssh executable. In WSL that configuration shadowed the default; the default should run instead.

So I had to run

git config --global --unset core.sshCommand
JBSnorro
  • 6,048
  • 3
  • 41
  • 62