[git] Git: Permission denied (publickey) fatal - Could not read from remote repository. while cloning Git repository

I am unable to clone a Git repository, and getting this error:

krishna.soni@KRISHNACHANDRAS /c/Projects $ git clone http://stage.abc.com:10088/pqr
http://<url>/<repository> Cloning into '<repository>'... fatal: could not read Username for 'http://<url>': No such file or directory

I've read Bitbucket, Windows and “fatal: could not read Password for”, but still have the problem.

I proceeded further but am now getting this error

sh.exe": chown: command not found

krishna.soni@KRISHNACHANDRAS /c
$ git clone ssh://[email protected].
/projects
Cloning into 'C:/projects'...
Permission denied (publickey).
fatal: Could not read from remote repository.

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

> krishna.soni@KRISHNACHANDRAS /c
>     $ git clone ssh:<url>
>     ts
>     Cloning into 'C:/projects'...
>     Permission denied (publickey).
>     fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

This question is related to git git-clone

The answer is


For me, when i wanted to clone from my repository, i had the same message noticed before "Permission denied (publickey) fatal: Could not read from remote repository". The solution for my case is to not use sudo before clone That's it.


I had a similar problem on linux. I solved the problem by logging into the github server and creating a deploy key. That's under settings for the repository. Then, I copied and pasted my public key (which is usually in ~/.ssh/id_rsa.pub, but your configuration might be different). There is a check box to give this key write access. Click on it (unless you are using git to deploy only, in which case, don't click on it).


Github(or Bitbucket) can't find your ssh key in their server.

Just add your key in your account setting.


If there is still issue around access after creating SSH and adding it in your Bitbucket account or wherever, you need to do the following in terminal on GitBash, OSX or Linux:

# Lists already added identities (‘ssh keys’)
ssh-add -l

Then, if you don't see your key listed, add it with the following (replace identity with its real name):

# Add a new identity
ssh-add ~/.ssh/identity

This worked for me.


I got the error...

Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

...when I was trying to make contact from a Git Bash prompt to Bitbucket after my laptop died from a bad Windows 10 update and was returned to factory settings. I had restored all my ssh files from a backup.

The cause of the error turned out to be a mismatch in my Windows account name after the computer rebuild. I learned that the public key file id_rsa.pub ends with an easily readable string comprising my Windows account name followed by an @ sign and then the computer name.

When I originally set up my computer, I created my Windows account name including my middle initial but when my computer was reset to factory settings, our new DevOps guy created my account name without my middle initial.

To resolve the issue, I simply edited my public key file id_rsa.pub and removed the middle initial in my name at the end. I had stuck with the same computer name so that was okay. I copied the file contents to the Windows clipboard. I then signed into Bitbucket, went to Bitbucket Settings under my avatar, and added a new public key where I pasted in the new contents.

Back at the Git Bash prompt, I confirmed that it now worked by entering the command...

ssh -T [email protected]

...and I got back a message that I was signed in.


I've had this issue on a Mac - while I setup SSH correctly to access my Git repository, after restart (and some time the Mac was on a standoff), all my credentials were removed. Apparently, for some reason the pub key was set to 644 which caused it to be removed from the keychain. To readd:

  • chmod 600 the public key
  • ssh-add ~/.ssh/[your private key] - this should display that identity has been added. The key file you want is the one without the .pub extension.
  • ssh-add -l should show you newly added identity

edit: apparently MacOS has tendency of removing keys - after downloading the High Sierra update (but I've not installed it yet) my key has been removed and I've had to add it again via ssh-add


After changing permissions of folder in which I was cloning, it worked:

sudo chown -R ubuntu:ubuntu /var/projects


I see this errormessage has a lot of different reasons. I had the same error when trying to set up locally another repository alongside one already fully working with ssh etc. and connect it to same server, but other repository. No answers were here to be found, but I figured it out. So I post it. Hopefully helps someone.

git remote add origin [email protected]:teamalpha/repositorytwo.git

You can try adding your ssh key to your private keychain. It worked for me

ssh-add -K ~/.ssh/[your-private-key]

I faced the same issue while running git clone command from windows command line. But the command runs successfully from Git Bash.


I had the same error. My solution was to logout and login again as my ssh-agent had become out of sync with the remote ssh key.

Adding this here just in case anyone has seen that the other answers don't seem to apply to their situation.


Useful thread and I don't bring much new to the table. The step I took is also part of the description Maxime helpfully points to but some may overlook it. The section is Adding your SSH key to the ssh-agent.

I had already generated my ssh-key but after a reboot I was unable to clone one of my own repositories.

I had to start the ssh-agent and add the private key to this agent in order to clone.

eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa

I followed the steps detailed in Generating a new SSH key and adding it to the ssh-agent and

Auto-launching ssh-agent on Git for Windows.

In my case ~/.profile file was not present in my Windows. I had to create it, then added the script provided in the above link.


I had the similar issue. I change the ssh keys and restarted and tried all other 'n' solutions. But the actual issue for me was our gitlab default protocol changed from ssh to https.

check the remote url with

git remote -v

change the remote url

git remote set-url origin https://URL

If you want to clone open source projects to submit a Pull Request:

I wanted to clone an open source project so I could submit some pull requests. The problem was that I didn't have permissions from the owner of the project. No matter, since my intention was to submit a PR, here is a viable alternative:

My solution:

  1. Fork Repository

Fork Repo

  1. Then clone from your forked repository. Clone from forked Repo

  2. Work on your features and then submit a pull request.


It may be stupid but it happened to us:

If you are using bitbucket and Sourcetree and you just copy paste the clone URL to the new repo dialog it will show the same error when pulling or pushing.

Make sure you delete the 'git clone' stuff before the URL.


If all fails, as was the case with me, Uninstall GIT, reinstall. For some reason this fixes.

P.S.

  1. I tried generating new Keys and Updating at SSH public keys
  2. Also tried deleting all keys and tried to create a fresh key.
  3. And NO, nothing wrong with my .gitconfig

I faced the same problem with GitHub. Here's how to solve it!

  1. Generate a SSH key on your computer as described here : Generating SSH keys.
  2. Login your GitHub account: GitHub Login.
  3. In your account, add the newly generated SSH Key: My account SSH keys.
  4. Try again to git clone the project.

Remove remote origin

git remote remove origin

Add HTTP remote origin


I was facing the same issue while setting up ssh for gitlab. I already have ssh for github and i could not overwrite that. The steps that worked for me are :

  1. Generate SSH with new path and add it to ssh list ssh-add /path/to/new/id_rsa.
  2. Create a file named config in ~/.ssh/ using. I used vi ~/.ssh/config/.
  3. Add this to the newly created file

# GitLab.com server Host gitlab.com RSAAuthentication yes IdentityFile /path/to/new/id_rsa

  1. Save and quit.

After that restart the terminal and try pushing, it should work


fix for hub cli tool:

  • git config --global hub.protocol https for long term
  • git remote add OOPS https://github.com/isomorphisms/go.git && git push OOPS for immediate fix

This error occurs with the hub command line tool because of their wrong default hub.protocol git-config value. They set repos to

git://github.com/schacon/ticgit.git

instead of what github actually accepts, namely https://github.com/schacon/ticgit.git.


Reading LESS=+/"HTTPS instead" man hub will explain where the above "long-term fix" command comes from.


You need to create a new ssh key by running ssh-keygen -t rsa.


For people that come here that are just trying to get the repository but don't care about the protocol (ssh / https), you might just want to use https instead of ssh (if it's supported).

So for example you use

git clone https://github.com/%REPOSITORYFOLDER%/%REPOSITORYNAME%.git

instead of

git clone [email protected]:%REPOSITORYFOLDER%/%REPOSITORYNAME%.git

If you generate new public ssh key and inserted it to bitbucket or github and

it not helped - please try to restart your PC. It helped me!!


While cloning, I had a similar issue [ my ERROR: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights .. etc ]

-- I was using bitBucket/UBUNTU14.04 in my case, but ALREADY had a set of key files that I had previously generated AND I had changed the name of the files. I simply COPIED the files to the standard id_rsa & id_rsa.pub name format. I then re-ran the command with out issue.

OBTW: I could have also used the password prompt by using the HTTP style clone.


In macOS / Linux (Ubuntu):

1. To Authenticate you need to add your public part of your SSH key pair to bitbucket from within your user settings: User Settings --> SSH keys

You will find the said public part in your ~/.ssh directory, usually id_rsa.pub . note the .pub part of the file name for Public. it will help you to generate one if you don't already have one

You are not done yet ...

2. You need to let your system know what key to use with which remote host, so add these lines to your ~/.ssh/config file

Host bitbucket.org
 IdentityFile ~/.ssh/PRIVATE_KEY_FILE_NAME

Where PRIVATE_KEY_FILE_NAME is the name of private part of your SSH key pair, if you haven't messed with it, usually its default name is : id_rsa in this case replace PRIVATE_KEY_FILE_NAME above with id_rsa(the private key DOES NOT have a .pub extension)


solution: On the server you are trying to clone to or push from cat ~/.ssh/id_rsa.pub Go to GitHub, settings, SSH and GPG Keys , New SSH key paste key.


if none of these answers help and

  1. you are using Windows
  2. you generate your key with Putty or you have installed Putty on your PC
  3. you generate your key with CMD or PowerShell

Try

  1. delete your keys
  2. search on windows for Git Bash, then generate a new key with Git Bash command line
  3. add public key to your online Repo

Then, you will find after git clone and type yes to confirm, it should begin to clone


I also faced the same issue. I did the following and it worked for me:

  1. Generate Keys from the GIT GUI client on Windows. Copy this key to the clipboard.
  2. Open up your account on bitBucket/ git website, and add this key to your profile. This way, the server knows that you are a legitimate user accessing from a legitimate system.
  3. That's it. All push commands worked for me henceforth.