83

I have the following and i need to clone the repository in either windows terminal command prompt or linux.

I tried as :

git clone [email protected]:xxx/xxx/git

I get

Permission denied(public key)
Couldn't read from remote repository

Also tried to change the URL as:

git clone https://xxxxx.com:xxx/xxx/git
2
  • Have you added your ~/.ssh/id_rsa.pub in your account profile (e.g. GitHub, BitBucket)?
    – Sajib Khan
    Jan 18, 2017 at 9:02
  • We dont have access to the account profile...The details are shared from the client and we just need to clone our code into their github
    – Sushivam
    Jan 18, 2017 at 11:34

4 Answers 4

231

Always coming late to answer anything, it may be possible that you have more than one ssh keys and if not specified git will try to use id_rsa but if you need a different one you could use

git clone [email protected]:userName/projectName.git --config core.sshCommand="ssh -i ~/location/to/private_ssh_key"

This way it will apply this config and use a key different than id_rsa before actually fetching any data from the git repository.

subsequent fetch or push will use the specified key to authenticate for the cloned repository.

Hope this is helpful to anyone.

5
  • 4
    But it makes problems with recursive submodules
    – rundekugel
    Sep 8, 2020 at 13:57
  • after cloning, initializing submodules does not work. is there any workaround? Oct 26, 2020 at 19:30
  • 2
    well, the problem with sub-module is a bit different, that the one I was initially trying to tackle, but basically you would need to override the .git/config file inside of each submodule and correct the sshCommand to whatever the submodule requires. you may get a better explanation reading the documentation here git-scm.com/docs/git-config#_configuration_file Oct 27, 2020 at 19:10
  • You can add this as a function to your ~/.profile and have git "aliases" specific for each repo/organization. function git-company(){git ${@:1} --config core.sshCommand="ssh -i ~/.ssh/company_private_key"} This allows use of git-company instead of git for repos that need company_private_key. All parameters are being passed transparently.
    – Draco
    Dec 17, 2020 at 15:46
  • I've found out (this is maybe specific to BitBucket) that the repo cannot be private in order to clone it even if the private SSH key is used.
    – jaques-sam
    Mar 9 at 8:55
61

I suggest that you follow those steps:

Step 1: Check for existing SSH keys

$> ls -al ~/.ssh

Do you see any files named id_rsa and id_rsa.pub?

If yes go to Step 3

If no, you need to generate them

Step 2: Generate a new SSH key

$> ssh-keygen -t rsa -b 4096 -C "yourEmail"

Add your SSH key to the ssh-agent

$> eval "$(ssh-agent -s)"

$> ssh-add ~/.ssh/id_rsa

Step 3.1: Add the SSH key to your GIT account.

Get your public key

$> cat ~/.ssh/id_rsa.pub

Go to your GIT project -> Settings -> SSH keys

Then past the content of your public key in SSH keys

Step 3.2: Force SSH Client To Use Given Private Key

This is an alternative solution when you can't set keys on your Git account

$> sudo nano ~/.ssh/config

Then change this line

IdentityFile <yourPrivateKey>

Step 4: Clone the project

$> git clone [email protected]:xxx/xxx/git

10
  • Is there alternate way to Step3 , cause we dont have access to GIT account, having only the specified details from my question , i guess i cant access the Git account... Also i have the ssh key content (i guess its id_rsa.pub contents)....so i created a folder, with the file name id_rsa.pub and pasted the contents i have...
    – Sushivam
    Jan 18, 2017 at 11:36
  • When you tape in a command cat ~/.ssh/config Do you see IdentityFile line? Jan 18, 2017 at 11:51
  • Here is the structure, i have a folder in my windows local drive as : C/Users/me/Test/.ssh in My ssh folder , i have .id_rsa file, and i when i type cat ~/.ssh/config i get no such file
    – Sushivam
    Jan 18, 2017 at 11:57
  • @Sachins I updated my answer: See Step 3.2: Force SSH Client To Use Given Private Key Jan 18, 2017 at 11:58
  • 1
    Please note that while id_rsa is the default given to ssh keys, they need not be named this. Mar 30, 2020 at 16:43
9

test ssh with GitHub to get actionable feedback.

in the command line run ssh -T [email protected]
(see https://help.github.com/articles/testing-your-ssh-connection/)


if that works, bother with git next. but likely there will, be your issue.

to clone, THEN use git clone [email protected]:$USERNAME/$REPONAME.git

1
  • 2
    Doesn't really address the question as asked. Apr 5, 2021 at 3:09
-12

To clone source code without using ssh, we can use http url with following format:

http://usernameWITHOUTdomain:password@the_rest_httpURL

ex: http://myname:[email protected]/projectname/project.git

3
  • 5
    This is using http, not ssh.
    – bschlueter
    Nov 11, 2020 at 22:41
  • 2
    Sure, but you said "To clone source code with Git using ssh", which is not what the example does.
    – bschlueter
    Nov 22, 2020 at 19:40
  • 1
    This does not follow ssh protocol - not a solution Apr 5, 2021 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.