685

I'm doing:

git clone ssh://[email protected]/home/user/private/repos/project_hub.git ./ 

I'm getting:

Fatal: destination path '.' already exists and is not an empty directory.

I know path . already exists. And I can assure that directory IS empty. (I do ls inside and I see nothing!)

What am I missing here in order to clone that project into the current directory ?

7
  • 21
    if you do a ls -a do you see a .git directory? Commented Mar 25, 2012 at 22:41
  • 3
    @dtryon - No. But I see a DS_Store whatever this is. Perhaps I should get rid of it. Thanks for that -a :s Commented Mar 25, 2012 at 22:43
  • @Thanks four your quick reply. James Maclaughlin that seems a beautiful command to make sure we clone to an empty directory. :) Commented Mar 25, 2012 at 22:49
  • 1
    I'm assuming then you are on a Mac. Does this help: stackoverflow.com/questions/107701/… Commented Mar 25, 2012 at 22:51
  • For what it's worth, any folder you browse to on a mac will have these little files created there. It's very annoying for both Windows users who are using the same shares, and for any system (e.g. git) that needs the folders to be empty or performs actions programmatically on every file in a folder. Commented Oct 25, 2012 at 10:00

22 Answers 22

733

simply put a dot next to it

git clone [email protected]:user/my-project.git . 

From git help clone:

Cloning into an existing directory is only allowed if the directory is empty.

So make sure the directory is empty (check with ls -a), otherwise the command will fail.

Sign up to request clarification or add additional context in comments.

22 Comments

agreed, note that on a mac, a .DS_Store file auto created by finder will block the clone. check with ls -la
Nope. This is not the right answer. This is would still return "fatal: destination path '.' already exists and is not an empty directory."
Works for me using git v1.8.3.2. @SidSarasvati Are you sure the current directory is empty?
@SidSarasvati well for me the directoy isn't empty, but I don't care about that so I'm not sure why Git would. Why can't git clone into a non-empty directory? Surely functionaly it's just a basic download.
mkdir creates hardlinks to . and .. by default so is a new directory technically "empty" in Unix-based systems, at all? I mean, I guess you could unlink . and unlink .. but that might cause you huge problems later if you forget to re-link again after cloning....
|
428

The following is probably not fully equivalent to a clone in all cases but did the trick for me:

git init . git remote add -t \* -f origin <repository-url> git checkout master 

In my case, this produces a .git/config file which is equivalent to the one I get when doing a clone.

9 Comments

Furthermore, this is the incantation that allows current content to remain - say if your cloning your dotfiles into your home directory.
With this I am finally allowed to clone into whichever folder I like without Git treating me like a baby. When I also added a temporary .gitignore containing * (ignore everything) I could perform git checkout master even though there was already some other files in the folder. Then all the commited files from the repository was cloned (and the temporary .gitignore was overwritten by the proper .gitignore from the repo). It all worked wonderfully. It should happen this way by itself by using git clone -f or something.
if you have files that are different/new/changed that will be written over (... files would be overwritten by checkout ... Aborting) use: git checkout master -f
The -t \* is unnecessary as it is the default.
git remote set-head origin -a might come handy. It sets origin's default branch (refs/remotes/origin/HEAD) as it is set in the remote repository. git clone does this automatically, unlike git remote add -f.
|
268

@Andrew has answered it clearly here. But as simple as this also works even if the directory is not empty:

git init . git remote add origin <repository-url> git pull origin master 

1 Comment

I'm happy to confirm <repository-url> can also be a local repo as git remote add origin /path/to/existing/repo
68

Do

git clone https://[email protected]/user/projectname.git . 

Directory must be empty

1 Comment

This answer looks to be a duplicate of stackoverflow.com/a/19243896
63

To be sure that you could clone the repo, go to any temporary directory and clone the project there:

git clone ssh://[email protected]/home/user/private/repos/project_hub.git 

This will clone your stuff into a project_hub directory.

Once the cloning has finished, you could move this directory wherever you want:

mv project_hub /path/to/new/location 

This is safe and doesn't require any magical stuff around.

2 Comments

This will create a subdirectory in the pre-existing /path/to/new/location directory which is certainly not the point of the question.
Then you will have to manually move the hidden files or use complicated command... and it does not answer the question.
58
git clone your-repo tmp && mv tmp/.git . && rm -rf tmp && git reset --hard 

1 Comment

this is a tweak :D
41

Specifing the absolute current path using $(pwd) worked for me.

git clone https://github.com/me/myproject.git $(pwd) 

git version: 2.21.0

Requires empty directory, as per the ticket description.


UPDATE 2022: You can now just use .

i.e.

git clone https://github.com/me/myproject.git . 

git version: 2.36.1

4 Comments

The folder needs to be empty, and is no different than providing a .
Thanks, but this was the solution provided in 2013 as well
Thanks @OneCricketeer - have updated the solution for v2.36.1 on Mac. Confirming this was an issue on 2.21.0 on Mac in 2019 when I left the response. If you can replicate that scenario, then please let me know.
Right. I'm just saying the version isn't relevant since that has always worked for almost a decade now. But it will not work for empty directories, mentioned in other answers, and was the actual error in the question, which you didn't address here (which I have replicated myself, using both of your answers here)
16

The solution was using the dot, so:

rm -rf .* && git clone ssh://[email protected]/home/user/private/repos/project_hub.git .` 

rm -rf .* && may be omitted if we are absolutely sure that the directory is empty.

Credits go to: @James McLaughlin on comments

5 Comments

This looks evil, since .* includes the parent directory! me:~/tmp/tmp/tmp$ ls -d .* . .. me:~/tmp/tmp/tmp$
This does not help, because I have dependencies in the directory where I need to checkout out.
I'm not sure it's wise to write rm -rf (in any form) into a SO answer without some REALLY SCARY warning sign. Some inexperienced user might come here looking for the "green check mark" (usually the best answer) then copy-pastes this command and poof...there goes his hard work in the current directory. BTW: rm -rf ./.* is "safer" if you just removing the hidden (dot) files and directories under the current dir (just as @stackunderflow stated before me). But rm -rf is a dangerous command for the inexperienced users so be careful with it! Just my 2 cents.
works on 1.7.1 The only folder inside was .git after git init (I knew it)
You don't need -rf to delete normal files. Please consider deleting this answer or at least accepting a different one.
14

If the current directory is empty, then this will work:

git clone <repository> foo; mv foo/* foo/.git* .; rmdir foo 

2 Comments

in my case, it worked even with some files in the '.' directory
Note that any dotfiles in directories under foo, eg. foo/bar/.foobar will not be moved with this command. See the answer from @phatblat
10

In addition to @StephaneDelcroix's answer, before using:

git clone [email protected]/my-project.git . 

make sure that your current dir is empty by using

ls -a 

2 Comments

Faced with this problem today. It turned out I had hidden .git and .gitignore folders in the directory I was trying to clone repository to. When I removed these folders everything was ok.
What would be command to empty current dir? I think that would be more useful than just checking it :)
10

I had this same need. In my case I had a standard web folder which is created by a web server install. For the purposes of this illustration let's say this is

/server/webroot

and webroot contains other standard files and folders. My repo just has the site specific files (html, javascript, CFML, etc.)

All I had to do was:

cd /server/webroot git init git pull [url to my repo.git] 

You need to be careful to do the git init in the target folder because if you do NOT one of two things will happen:

  1. The git pull will simply fail with a message about no git file, in my case:

fatal: Not a git repository (or any of the parent directories): .git

  1. If there is a .git file somewhere in the parent path to your folder your pulled repo will be created in THAT parent that contains the .git file. This happened to me and I was surprised by it ;-)

This did NOT disturb any of the "standard" files I have in my webroot folder but I did need to add them to the .gitignore file to prevent the inadvertent addition of them to subsequent commits.

This seems like an easy way to "clone" into a non-empty directory. If you don't want the .git and .gitignore files created by the pull, just delete them after the pull.

Comments

10

I used this to clone a repo to the current directory, which wasn't empty. Not necessarily clean living, but it was in a disposable docker container:

git clone https://github.com/myself/myRepo.git temp cp -r temp/* . rm -rf temp 

Here, I used cp -r instead of mv, since that copies hidden files and directories. Then dispose of the temporary directory with rm -rf

Comments

9

Further improving on @phatblat's answer:

git clone --no-checkout <repository> tmp \ && mv tmp/.git . \ && rmdir tmp \ && git checkout master 

as one liner:

git clone --no-checkout <repository> tmp && mv tmp/.git . && rmdir tmp && git checkout master

Comments

7

Improving on @GoZoner's answer:

git clone <repository> foo; shopt -s dotglob nullglob; mv foo/* .; rmdir foo 

The shopt command is taken from this SO answer and changes the behavior of the 'mv' command on Bash to include dotfiles, which you'll need to include the .git directory and any other hidden files.

Also note that this is only guaranteed to work as-is if the current directory (.) is empty, but it will work as long as none of the files in the cloned repo have the same name as files in the current directory. If you don't care what's in the current directory, you can add the -f (force) option to the 'mv' command.

Comments

3
shopt -s dotglob git clone ssh://[email protected]/home/user/private/repos/project_hub.git tmp && mv tmp/* . && rm -rf tmp 

2 Comments

Provide more information to explain why your code fixes the issue and what does it do.
"git clone ... tmp" - creates /tmp folder in the directory where the command is executed and clones the Git repository. Then "mv tmp/* ." - moves all the files from /tmp folder in the parent folder (where previous command has been executed) and finally "rm -rf tmp" - removes /tmp folder.
2
git clone ssh://[email protected]/home/user/private/repos/project_hub.git $(pwd) 

2 Comments

Note that this will still result in the same error posted in the question ("fatal: destination path 'resolved/path/of/pwd' already exists and is not an empty directory.") if you are doing this in a non-empty directory.
This solution did work for me running git version 2.25.0 on a Mac.
2

use . at the end of your command like below

git clone URL . 

2 Comments

This does not work on mac, it causes error: fatal: destination path '.' already exists and is not an empty directory.
For mac :: You should under parent directory and then use folder name instead of .(dot) like below git clone URL flodername Make sure that your folder is empty.
0

Here was what I found:

I see this:

fatal: destination path 'CouchPotatoServer' already exists and is not an empty directory. 

Amongst my searchings, I stumbled on to:

https://couchpota.to/forum/viewtopic.php?t=3943

Look for the entry by Clinton.Hall... If you try this (as I did), you will probably get the access denied response, there was my 1st clue, so the initial error (for me), was actually eluding to the wrong root issue.

Solution for this in windows: make sure you run cmd or git elevated, then run:

git clone https://github.com/RuudBurger/CouchPotatoServer.git 

The above was my issue and simply elevating worked for me.

Comments

-1

So I fixed this same error by deleting the hidden .git folder in my root directory, and then adding a period to the 'git clone repo .' in my root/dist folder. This is in the context of a vue-cli webpack project. So what everyone else is saying is right, it usually means you have git tracking either in the folder you are trying to clone into or in the parent folder or root of the folder in question!

Comments

-1

The solution for Windows is to clone the repository to other folder and then copy and paste to the original location or just copy the .git invisible folder.

1 Comment

Need to account for other hidden files like .env, for example
-2

Removing with

rm -rf .*

may get you into trouble or some more errors.

If you have /path/to/folder, and would like to remove everything inside, but not that folder, just run:

rm -rf /path/to/folder/*

Comments

-4

it's useful to create a new project directory by mkdir filename, then running the command of git clone xxxxx, and moving the files over

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.