Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git how to clone with SSH key, username

Tags:

git

clone

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 
like image 946
Sushivam Avatar asked Jan 18 '17 08:01

Sushivam


People also ask

How do I clone a git repository with username?

Clone the Repo If you want to clone it to a specific folder, just insert the folder address at the end like so: git clone https://<token>@github.com/<username>/<repository.git> <folder> , where <folder> is, you guessed it, the folder to clone it to! You can of course use . , .. , ~ , etc.

How do I clone a repository with username and password?

username and password. We can supply the username and password along with the git clone command in the remote repository url itself. The syntax of the git clone command with the http protocol is, git clone http[s]://host. xz[:port]/path/to/repo.

How do I use a specific SSH key?

To specify which private key should be used for connections to a particular remote host, use a text editor to create a ~/. ssh/config that includes the Host and IdentityFile keywords. Once you save the file, SSH will use the specified private key for future connections to that host.


2 Answers

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.

like image 149
Wiston Coronell Avatar answered Oct 14 '22 00:10

Wiston Coronell


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

like image 36
Ala Eddine JEBALI Avatar answered Oct 14 '22 00:10

Ala Eddine JEBALI