Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Clone Failure

Tags:

git

git-clone

I have a Linux-based Amazon AMI which houses a Git repository. I want to git clone that repository to my local OSX machine (which also has Git installed).

The repository lives on the Amazon box at /home/ec2-user/my_test_repo. Inside of the my_test_repo directory is the .git directory.

On my OSX machine I can successfully SSH to the machine hosting the repo as ec2-user and I can execute lots of bash commands. So, I know SSH works. However, the following command doesn't work when I execute it from my OSX machine:

git clone ssh://[email protected]/home/ec2-user/my_test_repo.git

I get the following error message:

Cloning into 'my_test_repo'...
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

Any ideas what I'm doing wrong here?

like image 946
filmnut Avatar asked May 20 '14 23:05

filmnut


2 Answers

The first problem is that you can't login to the server:

Cloning into 'my_test_repo'...
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

You need to get this command working first:

ssh [email protected]

This problem has nothing to do with Git, you need to get ssh working with public key authentication.

The second problem is that the path to your repository is probably wrong. If your repository on the server is in the .git directory inside /home/ec2-user/my_test_repo, then the URL would be:

git clone ssh://[email protected]/home/ec2-user/my_test_repo/.git

Notice the end part is my_test_repo/.git, because it should correspond to the filesystem path of the directory containing a Git repository. A Git repository contains files like HEAD, config, and directories like objects, refs, hooks, and some others.

As such, it looks like my_test_repo is a so-called working tree. If you clone from my_test_repo/.git, you won't be able to push to it, because git doesn't allow pushing to repositories with working tree. It only allows pushing to a so-called bare repository, without a working tree. You can create a bare repository from your existing non-bare repository with these commands:

git clone --bare my_test_repo my_test_repo.git

After you do this, your original URL should work, because now the path to the Git repository is really my_test_repo.git, instead of my_test_repo/.git. You don't need the my_test_repo working tree anymore, you can delete it.

Finally, you could simplify the repository URL like this:

git clone [email protected]:my_test_repo.git
like image 153
janos Avatar answered Nov 14 '22 21:11

janos


I finally got this working, with a lot of help from @janos (see his comments above).

First, you need to get ssh working with public key authentication. This has nothing to do with Git.

In Terminal, on the OSX client machine, I entered the following command: ssh-keygen -t dsa, which generates the SSH keys on my local OSX machine. For each one of the prompts, you can enter actual values, or just press ENTER (entering values allows you to customize and make the SSH process more secure). I just pressed ENTER.

Next,I copied the newly generated ~/.ssh/id_rsa.pub file (make 100% sure it's the file with .pub at the end) to the remote Amazon EC2 server. However, I found this process to be a little different than how most online tutorials explain (I think because of the somewhat unique use of .pem files for EC2 instances).

In Terminal on the OSX machine, I cd into the directory containing my .pem file, and then I entered the following command: scp -i JPo.pem ~/.ssh/id_rsa.pub [email protected]:.ssh/authorized_keys

At this point, ssh with public key authentication should now be working between the client OSX machine and the remote Amazon EC2 instance.

And now comes the Git stuff, which is pretty basic. I entered the following command: git clone ssh://[email protected]/home/ec2-user/my_test_repo/.git

And done. It worked!

like image 33
filmnut Avatar answered Nov 14 '22 21:11

filmnut