Git how to clone with SSH key, username

GitClone

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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

http://usernameWITHOUTdomain:password@the_rest_httpURL

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSushivamView Question on Stackoverflow
Solution 1 - GitWiston CoronellView Answer on Stackoverflow
Solution 2 - GitAla Eddine JEBALIView Answer on Stackoverflow
Solution 3 - GitDaij-DjanView Answer on Stackoverflow
Solution 4 - GitThien PhamView Answer on Stackoverflow