109

I'm trying to push a file to a git repo of a friend but errors on public key.

git push origin testbranch
Permission denied (publickey).
fatal: Could not read from remote repository.

Where and how do we define public / private keys?

git remote -v returns:

origin  git@github.com:Sesamzaad/NET.git (fetch)
origin  git@github.com:Sesamzaad/NET.git (push)

Any help is appreciated.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Wyguf Seak
  • 1,153
  • 2
  • 9
  • 8
  • 2
    possible duplicate of [github: newbie problems -> Permission denied (publickey). fatal: The remote end hung up unexpectedly](http://stackoverflow.com/questions/3617113/github-newbie-problems-permission-denied-publickey-fatal-the-remote-end) – AD7six Oct 29 '13 at 14:23
  • Hi @wyguf, Please accept my answer if it helped. :) Thanks – Shrutee May 29 '17 at 07:29
  • 3
    `\302\226git@github.com: Permission denied (publickey).` <- I needed to remove the spurious characters in my remote url that were added when I copied it from guthub. – jozxyqk Jul 13 '19 at 03:21
  • Luan Si Ho's solution worked for me. https://stackoverflow.com/a/43672761/7061796 – Umer Khalid Jun 14 '21 at 18:08

23 Answers23

161

I was facing same problem and here is what I did that worked for me.

Use ssh instead of http. Remove origin if its http.

git remote rm origin

Add ssh url

git remote add origin git@github.com:<username>/<repo>.git

Generate ssh key inside .ssh/ folder. It will ask for path and passphrase where you can just press enter and proceed.

cd ~/.ssh
ssh-keygen

Copy the key. You can view your key using -

cat ~/.ssh/id_rsa.pub

If you hadn't specified a different path then this will be the default path.

Add this key to your github account.

Next do -

ssh -T git@github.com

You will get a welcome message in your console.

cd into to your project folder. git push -u origin master now works!

Shrutee
  • 1,805
  • 1
  • 12
  • 13
  • 2
    I got the welcome message but `git push -u origin master` still doesn't work – Hack-R Nov 14 '16 at 16:56
  • 1
    Steps `git remote rm` and `git remote add` only was enough in my case. – Stephan Ahlf Mar 16 '17 at 19:35
  • 8
    I tried it on a Gid Bash "cat ~/.ssh/id_rsa.pub" and "ssh -T git@github.com". I get only "git@github.com: Permission denied (publickey)." Please help. – user2301515 Mar 21 '20 at 06:34
  • @user2301515 Same problem here. I changed to https and then pushed but when it asks for your password input your personal access token instead (assuming you have one). It pushed then. See https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories#switching-remote-urls-from-ssh-to-https I would still love to know why I cant use my ssh key though. – Frikster Sep 06 '21 at 20:10
63

I just had to deal with this issue. @user3445140's answer helped me, but was much more than I needed to do.

  1. Get your public SSH key with cat ~/.ssh/id_rsa.pub
  2. Copy the key, including the "ssh-rsa" but excluding your computer name at the end
  3. Go to https://github.com/settings/ssh
  4. Add your SSH key
steel
  • 11,883
  • 7
  • 72
  • 109
33

This worked for me.

first of all, remove current remote :

git remote rm origin

second, add remote through HTTPS but git@xxx :

git remote add origin https://github.com/Sesamzaad/NET.git

then push has worked for me :

git push origin master
Mat M
  • 1,786
  • 24
  • 30
Cloud13th
  • 438
  • 4
  • 4
25

I fixed it by re-adding the key to my ssh-agent.

with the following command:

ssh-add ~/.ssh/path_to_private_key_you_generated

For some reasons it was gone.

Wis
  • 484
  • 7
  • 22
Bennet G.
  • 537
  • 8
  • 14
  • 10
    `ssh-add -k ~/.ssh/id_rsa` did it for me – Dbz Sep 19 '18 at 20:01
  • I had to start ssh agent even tho Putty was ont he background doing: `$ eval "$(ssh-agent -s)"` – Nekeniehl Aug 20 '20 at 15:52
  • Use `--apple-use-keychain` for newer MacOS versions like Ventura, see https://docs.github.com/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent – AnhellO Mar 13 '23 at 08:01
16

I am running Ubuntu 16.04

Removing the remote origin using

git remote rm origin

setting the http url using

git remote add origin https://github.com/<<Entire Path of the new Repo>>

git push origin master

Above steps successfully added code to repo.

vignesh787
  • 199
  • 1
  • 7
6

None of the above solutions worked for me. For context, I'm running ubuntu, and I had already gone through the ssh-key setup documentation. The fix for me was to run ssh-add in the terminal. This fixed the issue.

Source: http://baptiste-wicht.com/posts/2010/07/tip-how-to-solve-agent-admitted-failure-to-sign-using-the-key-error.html

vsahu
  • 170
  • 2
  • 6
5

This worked for me. Simplest solution by far.

If you are using GitHub for Windows and getting this error, the problem might be that you are trying to run the command in the wrong shell or mode. If you are trying to do git push origin master in the regular command prompt or PowerShell, this is the problem.

You need to do it in a git shell. Simply open Github for Windows, right click, and select "Open Shell Here". It looks like a regular PowerShell window, but it's not, which makes it really confusing for newbies to git, like myself.

I hope others find this useful.

dberm22
  • 3,187
  • 1
  • 31
  • 46
  • 1
    Thanks. I Was going nuts because for me `ssh -T git@github.com` worked but not `push origin master`. And that was because I was doing it in a cygwin64 window instead of the git bash thing... – 2072 Jul 07 '19 at 18:36
4

The documentation from Github is really explanatory.

https://help.github.com/en/articles/adding-a-new-ssh-key-to-your-github-account https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

I think you must do the lasts steps from the guide to proper configure your keys

$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_rsa
antikytheraton
  • 631
  • 7
  • 8
  • 1
    I had to do the following after your steps to get it working: git remote set-url origin git@github.com:UserName/AppName.git – Tracy Zhou Jun 12 '19 at 17:29
3

To make things clear, there are two ways to connect to your git repository from your PC:

  1. Using SSH which has the following format : git@github.com:username/repository-name.git

  2. Using HTTPS which has the following format: https://github.com/username/repository-name.git

You have the absolute right to choose whichever is best for you.

If you choose to go for (1) then you have to establish the SSH connection between your PC and git account (find the steps from the answers above to know how to do it).

Alternatively, you can choose (2) and connect via HTTPS which does not require you to establish SSH on your PC, so you won't get the permission denied (public key) issue as long as your git account is already verified on the PC and connected.

binmosa
  • 967
  • 1
  • 7
  • 9
2

If ssh key is already generated in your git account, then just do the following-

ssh-add -k ~/.ssh/id_rsa
Natasha
  • 6,651
  • 3
  • 36
  • 58
2

May be, if another answers don't help try to run git remote -v

If you see:

origin  https://github.com/<name/repo.git> (fetch)
origin  https://github.com/<name/repo.git> (push)

(origin starts with https or http)

Run git remote set-url origin git@github.com:<name/repo.git>

and than git push again

  • This doesn't answer the question. It already says it's using that form instead of https. And if on https, it skips the publickey check altogether and instead asks for the username/password. – Jerven Clark Aug 14 '21 at 06:42
1

You probably have to add your public key to github. https://help.github.com/articles/generating-ssh-keys

Check this thread: GitHub: Permission denied (publickey). fatal: The remote end hung up unexpectedly

Community
  • 1
  • 1
Walter B
  • 655
  • 6
  • 8
1

This error happened while using Ubuntu Bash on Windows.

I switched to standard windows cmd prompt, and it worked no error.

This is a workaround as it means you probably need to load the ssh private key in ubuntu environment if you want to use ubuntu.

Andrew
  • 18,680
  • 13
  • 103
  • 118
  • I recently moved to windows and was facing this issue using powershell in vscode and git-bash. I copied the url directly from github into the windows-cmd and magically worked, then I copied the same command that worked in cmd to git-bash and it worked again with no explanation. – futureapricot May 15 '21 at 19:44
1

Solution : you have to add you ssh key in your git-hub profile. Follow steps to solve this problem

  1. Right Click Folder you want to push in git
  2. Select git-bash here problem
  3. Write command ssh-keygen by this command your key is generated
  4. Copy the key from cmd or go to (C:/User/your_user/.ssh/)
  5. open id.rsa with notepad.
  6. Copy your key
  7. Now go to your git-hub profile
  8. Go to settings
  9. select SSH and Gpg keys
  10. select New ssh key option
  11. add window-key in the title
  12. Paste your key in the description part below title field
  13. Save

Now you are ready to push your folder

  1. Now go to folder you want to upload
  2. right click on the folder
  3. Select git bash here
  4. git init
  5. git add README.md
  6. git commit -m "first commit"
  7. git remote add origin https://github.com//
  8. git push -u origin master

Hope this will be Helpful for you

Sohaib Anwaar
  • 1,517
  • 1
  • 12
  • 29
1

If you have your private key(s) in ~/.ssh and have added them to https://github.com/settings/ssh, but still are unable to commit to a Github repo added via ssh, make sure they are added to your ssh-agent:

ssh-add -k ~/.ssh/[PRIVATE_KEY]

You can add multiple private keys for multiple servers (e.g. Bitbucket & GitHub) and it will use the correct one when dealing with git.

Морт
  • 1,163
  • 9
  • 18
1

I can ssh the connect git@github.com, and return the successful window, but cannot push anything to github, the server return that:

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

how I solve the question:

delete the origin and re-add it:

git remote rm origin
git remote add origin git@github.com:<xxxx>/<xxxx>.git
git branch -M main
git push -u origin main

the terminal returns:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 321 bytes | 321.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:ShowTimeWalker/MyFirstTrailForGit.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
Clément Joly
  • 858
  • 9
  • 27
0

If you already have your public key added to the GITHUB server there are other solutions that you can try.

In my case the GIT PUSH was failing from inside RUBYMINE but doing it from the Terminal window solved the problem.

For more solutions visit this page https://github.com/gitlabhq/gitlabhq/issues/4730

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
0

In order to deploy to your friend's repo you need to add your public key to the repository's deploy keys.

Go to the repository, go to deploy keys, and add the id_rsa.pub (or whatever yours is named) to "deploy keys".

I believe adding the key to your own account only lets you write to repositories that your account created. If it was created by an organization you need to add the key to the repo's deploy keys.

https://developer.github.com/v3/guides/managing-deploy-keys/

the tao
  • 253
  • 2
  • 6
  • 13
0

I faced the same problem.Ask your friend to add you as a collaborator by going to his repo settings and adding a new collaborator.

You will recieve an invite email ,accept it.Then you are good to go. Just make sure that you have added right remote.

0

You need to fork the project to your own user repository.

Then add origin:

git remote add upstream your-ssh-here
git fetch upstream
git branch --set-upstream-to=upstream/master master
Boken
  • 4,825
  • 10
  • 32
  • 42
Tal Hakmon
  • 475
  • 5
  • 6
0

Set global variable

export GIT_SSH_COMMAND="ssh -v"

Now you see which keys are used. E.g. with

git remote show origin

If you have the same problem as i do, you see

debug1: send_pubkey_test: no mutual signature algorithm

This can be solved with

PubkeyAcceptedKeyTypes +ssh-rsa

in your

~/.ssh/config

or by generating a better key.

-1

If you are getting 403 error here is the solution:

 The requested URL returned error: 403

As you are having your account registered with another account so you need to remove the github credentials from windows

control panel > user accounts > credential manager > Windows credentials > Generic credentials

then remove the Github keys
MJ X
  • 8,506
  • 12
  • 74
  • 99
-1

For windows 10

  1. In the command prompt, type the following: ssh-keygen

  2. By default, the system will save the keys to C:\Users\your_username/.ssh/id_rsa. You can use the default name, or you can choose more descriptive names. This can help distinguish between keys, if you are using multiple key pairs. To stick to the default option, press Enter.

Note: If a file with the same name already exists, you will be asked whether you want to overwrite the file.

  1. You’ll be asked to enter a passphrase. Hit Enter to skip this step.

  2. The system will generate the key pair, and display the key fingerprint and a randomart image.

  3. Open your file browser.

  4. Navigate to C:\Users\your_username/.ssh.

  5. You should see two files. The identification is saved in the id_rsa file and the public key is labeled id_rsa.pub. This is your SSH key pair.

  6. Copy the key, including the "ssh-rsa" but excluding your computer name at the end

  7. Go to https://github.com/settings/ssh Add your SSH key

  8. Go to repo folder and add your user name by using this command, type the following: git remote add origin git@github.com:git_hub_user_name/your_repo_name.git

  9. ssh -T git@github.com

Now you should see success message.

Prasaathviki
  • 1,147
  • 2
  • 11
  • 22