329

I have a project on which I created a git repository:

$ cd myproject  
$ git init  
$ git add .  
$ git commit  

I the wanted to create a bare clone on another machine:

$ cd ..  
$ git clone --bare myproject  ssh://user@server:/GitRepos/myproject.git  

I executed the clone but did not print any answer. I logged on to the server machine and tried to see how the files are stored. The path /GitRepos was empty, so I decided to do the clone again:

$ git clone --bare myproject  ssh://user@server:/GitRepos/myproject.git

This time the answer was :

fatal: destination path 'ssh://user@server:/GitRepos/myproject.git' already exists and is not an empty directory.

But I saw that the path was empty.
What's going on here ?

4
  • I don't think running git clone --bare will do what you think it will do.
    – Wolfer
    Commented Feb 27, 2014 at 17:36
  • 3
    For me, the ssh:// created a folder, instead of using a remote protocol. Got things to work using @alec-the-geek's answer. Would you mind selecting a preferred answer for this - it's your job.
    – akauppi
    Commented Mar 15, 2015 at 12:29
  • 1
    @akauppi: It is the OP's responsibility to consider all answers, but they are under no obligation to select an answer till they are satisfied. (Of course, it doesn't hurt to remind them or ask what further info they need for an answer to be acceptable to them!)
    – jvriesem
    Commented Sep 17, 2015 at 4:14
  • @jvriesem of course. Would remove the "it's your job" now. But it's an old thing... :)
    – akauppi
    Commented Sep 18, 2015 at 9:05

10 Answers 10

285

This is possibly unrelated directly to the question; but one mistake I just made myself, and I see in the OP, is the URL specification ssh://user@server:/GitRepos/myproject.git - namely, you have both a colon :, and a forward slash / after it signifying an absolute path.

I then found Git clone, ssh: Could not resolve hostname – git , development – Nicolas Kuttler (as that was the error I was getting, on git version 1.7.9.5), noting:

The problem with the command I used initially was that I tried to use an scp-like syntax.

... which was also my problem! So basically in git with ssh, you either use

  • ssh://[email protected]/absolute/path/to/repo.git/ - just a forward slash for absolute path on server
  • [email protected]:relative/path/to/repo.git/ - just a colon (it mustn't have the ssh:// for relative path on server (relative to home dir of username on server machine)
4
  • 99
    This doesn't work. If you want to specify a relative path with ssh, you have to lose the ssh:// prefix. I just spent 20 minutes trying to figure this out. git clone [email protected]:relative/path/to/repo.git/ should work.
    – bobbaluba
    Commented Jun 13, 2013 at 1:29
  • 3
    Tried it with git 2.1.4 on a Debian 8. Either sugestions do not work. No directory was created under /GitRepos on first attempt, and on 2nd attempt complained that the path already existed. Just like the original problem reported
    – dlsa
    Commented Jul 30, 2015 at 17:14
  • 1
    git remote add local username@server:~/directory/repository_folder/ This worked perfectly for me. Commented Nov 9, 2020 at 10:53
  • 2
    For a relative path I used: git remote add my-remote-name ssh://[email protected]/~/MyGitRepo.git
    – Benjohn
    Commented Feb 1 at 18:40
204

For repositories on GitHub, try:

git clone ssh://[email protected]/<user>/<repository name>.git

For setting up git to clone via ssh see:

2
  • 1
    is the <user> my username in github or my email address for the github? becaause this link suggests me to use the email address during the public key generation. help.github.com/en/github/authenticating-to-github/… after trying that (empty passphrase for the ssh key gen), I still could not clone repo from github. p.s. I did not add the key to the agent. Commented Oct 22, 2019 at 18:07
  • 1
    same works for gitlab also. prior to that, we need to enable the SSH key setup, as you have suggested Commented Apr 20, 2020 at 9:34
54

You need to run the clone command on what you are calling the server. But I bet you are not running an ssh server on your local client so that won't work anyway. Suggest you follow this approach (check the manual 'cause I'm doing this from memory)

  1. Log into the server machine.
  2. Create a bare repo using git init --bare
  3. On the client machine you can push your repo to the server. git remote add origin ssh://user@server:/GitRepos/myproject.git followed by git push origin master
2
  • Great advice! Some notes for newcomers (like me) who haven't dealt with pure-server-git-folders before: 2. cd /GitRepos; mkdir myproject.git; cd myproject.git before the git init --bare. You won't get a work copy here - the repo files and folders (normally in .git) will be bare out for you, thus the name of the flag. Thanks
    – akauppi
    Commented Mar 15, 2015 at 12:23
  • 3
    "git init --bare" does not create a .git directory. It puts the files directly on the folder. When adding the remote origin I get : ""fatal: Not a git repository (or any parent up to mount point /home) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
    – dlsa
    Commented Jul 30, 2015 at 17:16
32

Disclaimer: This is just a copy of a comment by bobbaluba made more visible for future visitors. It helped me more than any other answer.


You have to drop the ssh:// prefix when using git clone as an example

git clone [email protected]:owner/repo.git
30

Easy way to do this issue
try this.

Step 1:

ls -al ~/.ssh

enter image description here

Step 2:

ssh-keygen 

(using enter key for default value) enter image description here Step 3: To setup config file

vim /c/Users/Willie/.ssh/config

Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa

Step 4:

git clone [email protected]:<username>/test2.git

enter image description here

Step 5:
When you finished Step 4
1.the test2.git file will be download done
2.you will get the new file(known_hosts) in the ~/.ssh
enter image description here

PS: I create the id_rsa and id_rsa.ub by meself and I deliver it to the Gitlab server. using both keys to any client-sides(windows and Linux).

5

Git 101:

git is a decentralized version control system. You do not necessary need a server to get up and running with git. Still you might want to do that as it looks cool, right? (It's also useful if you want to work on a single project from multiple computers.)

So to get a "server" running you need to run git init --bare <your_project>.git as this will create an empty repository, which you can then import on your machines without having to muck around in config files in your .git dir.

After this you could clone the repo on your clients as it is supposed to work, but I found that some clients (namely git-gui) will fail to clone a repo that is completely empty. To work around this you need to run cd <your_project>.git && touch <some_random_file> && git add <some_random_file> && git commit && git push origin master. (Note that you might need to configure your username and email for that machine's git if you hadn't done so in the past. The actual commands to run will be in the error message you get so I'll just omit them.)

So at this point you can clone the repository to any machine simply by running git clone <user>@<server>:<relative_path><your_project>.git. (As others have pointed out you might need to prefix it with ssh:// if you use the absolute path.) This assumes that you can already log in from your client to the server. (You'll also get bonus points for setting up a config file and keys for ssh, if you intend to push a lot of stuff to the remote server.)

Some relevant links:
This pretty much tells you what you need to know.
And this is for those who know the basic workings of git but sometimes forget the exact syntax.

1
  • 1
    Both of those now links appear to be dead or not showing what you expected them to.
    – Todd
    Commented Jan 16, 2018 at 17:33
3

I want to attempt an answer that includes git-flow, and three 'points' or use-cases, the git central repository, the local development and the production machine. This is not well tested.

I am giving incredibly specific commands. Instead of saying <your folder>, I will say /root/git. The only place where I am changing the original command is replacing my specific server name with example.com. I will explain the folders purpose is so you can adjust it accordingly. Please let me know of any confusion and I will update the answer.

The git version on the server is 1.7.1. The server is CentOS 6.3 (Final).

The git version on the development machine is 1.8.1.1. This is Mac OS X 10.8.4.

The central repository and the production machine are on the same machine.

the central repository, which svn users can related to as 'server' is configured as follows. I have a folder /root/git where I keep all my git repositories. I want to create a git repository for a project I call 'flowers'.

cd /root/git
git clone --bare flowers flowers.git

The git command gave two messages:

Initialized empty Git repository in /root/git/flowers.git/
warning: You appear to have cloned an empty repository.

Nothing to worry about.

On the development machine is configured as follows. I have a folder /home/kinjal/Sites where I put all my projects. I now want to get the central git repository.

cd /home/kinjal/Sites
git clone [email protected]:/root/git/flowers.git

This gets me to a point where I can start adding stuff to it. I first set up git flow

git flow init -d

By default this is on branch develop. I add my code here, now. Then I need to commit to the central git repository.

git add .
git commit -am 'initial'
git push

At this point it pushed to the develop branch. I want to also add this to the master branch.

git flow release start v0.0.0 develop
git flow release finish v0.0.0
git push

Note that I did nothing between the release start and release finish. And when I did the release finish I was prompted to edit two files. This pushed the develop branch to master.

On the production site, which is on the same machine as my central git repository, I want put the repository in /var/www/vhosts/example.net. I already have /var/www/vhosts.

cd /var/www/vhosts
git clone file:///root/git/flowers.git example.net

If the production machine would also be on a different machine, the git clone command would look like the one used on the development machine.

2

git clone git@server:Example/proyect.git

0
2

Upfront, I am a bit lacking in my GIT skills.

That is going to clone a bare repository on your machine, which only contains the folders within .git which is a hidden directory. execute ls -al and you should see .git or cd .git inside your repository.

1
  • I misinterpreted the command. What I did was creating a subdirectory tree ssh:/myproject.git under the current directory. I thought I was cloning to a remote directory, but what the command really does is clone from a repository.
    – dlsa
    Commented May 29, 2011 at 13:51
1

I did : git clone --bare "/GITREPOSITORIES/RepoA" "ssh://luc@EERSTENASDS119J/volume1/RepoA" Result : fatal: destination path 'ssh://luc@EERSTENASDS119J/volume1/RepoA' already exists and is not an empty directory.

The system created a directory ssh://luc@EERSTENASDS119J/volume1/RepoA in my current path.

So git clone did not interpret the URL specification. Used the workaround of Alec.

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