643

I'm doing:

git clone ssh://user@host.com/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 ?

MEM
  • 30,529
  • 42
  • 121
  • 191
  • 18
    if you do a `ls -a` do you see a `.git` directory? – Davin Tryon Mar 25 '12 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 – MEM Mar 25 '12 at 22:43
  • @Thanks four your quick reply. James Maclaughlin that seems a beautiful command to make sure we clone to an empty directory. :) – MEM Mar 25 '12 at 22:49
  • 1
    I'm assuming then you are [on a Mac](http://en.wikipedia.org/wiki/.DS_Store). Does this help: http://stackoverflow.com/questions/107701/how-can-i-remove-ds-store-files-from-a-git-repository – Davin Tryon Mar 25 '12 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. – jsims281 Oct 25 '12 at 10:00
  • Possible duplicate of [What's the best practice to "git clone" into an existing folder?](http://stackoverflow.com/questions/5377960/whats-the-best-practice-to-git-clone-into-an-existing-folder) – 7ochem Feb 22 '16 at 11:42
  • Does this answer your question? [How do you clone a Git repository into a specific folder?](https://stackoverflow.com/questions/651038/how-do-you-clone-a-git-repository-into-a-specific-folder) – Liam Nov 30 '20 at 15:24

22 Answers22

679

simply put a dot next to it

git clone git@github.com: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.

mota
  • 5,275
  • 5
  • 34
  • 44
Roshan Pal
  • 7,438
  • 2
  • 12
  • 12
  • 18
    agreed, note that on a mac, a .DS_Store file auto created by finder will block the clone. check with `ls -la` – ptim Nov 01 '13 at 06:08
  • 328
    Nope. This is not the right answer. This is would still return "fatal: destination path '.' already exists and is not an empty directory." – Sid Sarasvati Dec 08 '13 at 22:45
  • 18
    Works for me using git v1.8.3.2. @SidSarasvati Are you sure the current directory is *empty*? – Wesley Baugh Feb 02 '14 at 04:35
  • 5
    @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. – Nathan Hornby Apr 24 '14 at 12:19
  • 1
    No, it's not actually. Among other things, a full git working tree is initialized and checked out. You could imagine strange things could be happening when doing that in a non-empty directory. Apart from that, it's a *clone*, which IMHO means the end-result should always be the same. If you intend for the files that are already there to get included into your git repository, you can move them away temporarily and copy them back in. – Jaap Haagmans Jun 11 '14 at 09:20
  • 1
    Works for me. Just remember to delete any .DS_Store files – ChrisRich Jan 08 '15 at 03:01
  • 1
    Initially I kept getting the error. I found that when I ran `ls -lah` there were a couple of `.` files - such as a `.gitignore` file left over from a previous clone to the same folder - which I had to remove first (these weren't visible with a regular `ls`, so it took me a moment to work out). After removing these, my clone worked. – Aaron Newton Apr 29 '15 at 11:32
  • To second @SidSarasvati, this does not work in git version 2.1.4 (on Cygwin). Instead recommend to follow answer from @ AndreHolzner. – kevinarpe May 12 '15 at 11:46
  • @SidSarasvati From `git help clone`: "Cloning into an existing directory is only allowed if the directory is empty." The answer is correct, but the directory must be empty. – mota May 31 '15 at 21:07
  • 4
    `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.... – Seldom 'Where's Monica' Needy Jul 23 '15 at 19:41
  • 2
    Cloning to empty directory is close to useless, you could just as well remove the directory itself and recreate it by cloning. In my opinion the right answer would have to address non-empty directories. – Pavel Šimerda Jan 11 '16 at 08:28
  • Try `ls -l -a` and see if there are any hidden files that you need to remove such as `.gitignore` and `.git/` before you clone. – MattCochrane Jul 15 '16 at 05:18
  • 2
    Did you even read the question? OP already tried that. He is looking for a solution that works with non-empty directories. – Navin Jul 28 '16 at 21:42
  • 1
    +1 for illustrating a vital and oft overlooked stipulation of git: `Cloning into an existing directory is only allowed if the directory is empty.` – Steve Benner Nov 05 '16 at 01:22
  • Be sure to delete all hidden files before this. Including .git directory – Mladen Janjetovic Nov 15 '17 at 11:29
  • in my case the issue was a hidden `.DS_Store` file on macOS – Vladyslav Zavalykhatko Feb 10 '18 at 12:14
  • @cowbert How does this not answer the OP? It turned out that OP had a DS_Store file in the directory that appeared empty... – aross Feb 15 '18 at 11:23
  • @aross because the OP was looking for a solution to allow cloning into an non-empty directory and 2 other answers actually answer that. There's an implicit "X/Y question" involved here. – cowbert Feb 15 '18 at 13:53
  • @cowbert Implicit is the keyword here. The explicit question was cloning into an *empty* directory. And this answer has a command that tells you whether you really had an empty directory to begin with. That's the X/Y in my opinion and that's also how OP found his problem and was able to solve it. – aross Feb 15 '18 at 14:02
  • @aross he also explicitly posted the error message "Fatal: destination path '.' already exists and is not an empty directory.". There is a general answer on how to deal with this exception deterministically in all cases and that is init a repo and checkout the remote. – cowbert Feb 15 '18 at 14:07
  • @cowbert ... and then accidentally commit DS_Store – aross Feb 15 '18 at 15:19
  • running `rm -rf * .[!.]*` should solve this. Be aware this will delete all hidden files. – Amit Sep 14 '21 at 14:26
410

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.

Grigor Yosifov
  • 1,424
  • 1
  • 10
  • 13
Andre Holzner
  • 18,333
  • 6
  • 54
  • 63
  • 47
    Furthermore, this is the incantation that allows current content to remain - say if your cloning your dotfiles into your home directory. – rbellamy Apr 14 '14 at 16:33
  • 7
    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. – PaulMag Oct 23 '14 at 15:37
  • 1
    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 – visualex Jan 13 '15 at 09:35
  • If I want to keep the existing content in this directory, should I set the files/folders in .git/info/exclude or is there a better way? They aren't relevent to the repo so I don't want to put them in .gitignore. I won't be pushing from here anyway - just pulling to keep it updated. Or do I even need to worry about ignoring them? – Syntax Error Aug 19 '15 at 16:16
  • 4
    The `-t \*` is unnecessary as it is the default. – Palec Jun 05 '17 at 08:02
  • 1
    `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`. – Palec Jun 05 '17 at 08:38
  • @Palec I was not able to get the original answer to work without `-t \*` on git version `2.13.3.windows.1` on git-bash. – user151841 Jul 28 '17 at 15:10
  • Hi, folks. I know this question is old, but I followed these instructions to put files into an empty repository. At `get checkout master`, I got `error: pathspec 'master' did not match any file(s) known to git.`. I went boldly forward and did `git add .`, `git commit -m "Write a commit message!"`, and `git push origin master`. The output I got was `* [new branch] master -> master`. Is that right? – Becca Dee Sep 14 '18 at 21:24
  • @Don01001100 make sure you had `-f` in `git remote add -f ...` Otherwise, make sure that `master` branch also actually exists on remote. – Paul Jan 15 '22 at 02:55
260

@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
Community
  • 1
  • 1
ambes
  • 5,272
  • 4
  • 26
  • 36
  • 2
    I'm happy to confirm `` can also be a local repo as `git remote add origin /path/to/existing/repo` – krubo Oct 04 '19 at 18:20
71

Do

git clone https://user@bitbucket.org/user/projectname.git .

Directory must be empty

NSukonny
  • 1,090
  • 11
  • 18
64

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

git clone ssh://user@host.com/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.

eckes
  • 64,417
  • 29
  • 168
  • 201
  • 12
    This will create a subdirectory in the pre-existing `/path/to/new/location` directory which is certainly not the point of the question. – Pavel Šimerda Jan 11 '16 at 08:31
  • 1
    Then you will have to manually move the hidden files or use complicated command... and it does not answer the question. – Black Jan 30 '20 at 09:57
58
git clone your-repo tmp && mv tmp/.git . && rm -rf tmp && git reset --hard
return1.at
  • 2,992
  • 3
  • 23
  • 26
37

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

Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
  • The folder needs to be empty, and is no different than providing a `.` – OneCricketeer Jun 07 '22 at 03:19
  • Thanks, but this was the [solution provided in 2013](https://stackoverflow.com/a/19243896/2308683) as well – OneCricketeer Jun 08 '22 at 15:31
  • 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. – Nick Grealy Jun 08 '22 at 23:36
  • 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) – OneCricketeer Jun 09 '22 at 14:58
14

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

git clone <repository> foo; mv foo/* foo/.git* .; rmdir foo
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • 1
    in my case, it worked even with some files in the '.' directory – OSdave Apr 22 '12 at 18:10
  • 2
    Note that any dotfiles in directories under foo, eg. foo/bar/.foobar will not be moved with this command. See the answer from @phatblat – lea Jan 15 '15 at 00:17
13

The solution was using the dot, so:

rm -rf .* && git clone ssh://user@host.com/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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
MEM
  • 30,529
  • 42
  • 121
  • 191
  • 12
    This looks evil, since .* includes the parent directory! me:~/tmp/tmp/tmp$ `ls -d .*` `. ..` me:~/tmp/tmp/tmp$ – stackunderflow Dec 10 '13 at 11:47
  • 1
    This does not help, because I have dependencies in the directory where I need to checkout out. – b01 Jan 09 '14 at 13:38
  • 23
    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. – Andrew Aug 02 '14 at 14:12
  • works on 1.7.1 The only folder inside was `.git` after `git init` (I knew it) – vladkras Sep 18 '15 at 13:44
  • 3
    You don't need `-rf` to delete normal files. Please consider deleting this answer or at least accepting a different one. – Navin Jul 28 '16 at 22:42
10

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

git clone git@github.com.user/my-project.git .

make sure that your current dir is empty by using

ls -a
Jakehao
  • 1,019
  • 8
  • 15
  • 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. – Tamara Apr 07 '15 at 21:00
  • What would be command to empty current dir? I think that would be more useful than just checking it :) – Adam Pietrasiak Apr 19 '16 at 19:25
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.

Kukic Vladimir
  • 1,010
  • 4
  • 15
  • 22
igeocacher
  • 129
  • 1
  • 2
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

RexBarker
  • 1,456
  • 16
  • 14
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

166_MMX
  • 590
  • 5
  • 18
6

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.

Community
  • 1
  • 1
phatblat
  • 3,804
  • 3
  • 33
  • 31
3
shopt -s dotglob
git clone ssh://user@host.com/home/user/private/repos/project_hub.git tmp && mv tmp/* . && rm -rf tmp
John Josef
  • 314
  • 2
  • 5
  • 4
    Provide more information to explain why your code fixes the issue and what does it do. – Martin Jun 15 '17 at 18:19
  • "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. – Marinski Dec 08 '18 at 10:02
2

use . at the end of your command like below

git clone URL .
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Upendra
  • 683
  • 6
  • 23
  • 2
    This does not work on mac, it causes error: `fatal: destination path '.' already exists and is not an empty directory.` – Pedro Luz May 06 '17 at 13:05
  • 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. – Upendra May 08 '17 at 05:55
2
git clone ssh://user@host.com/home/user/private/repos/project_hub.git $(pwd)
Catalin
  • 135
  • 1
  • 7
  • 2
    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. – Gino Mempin May 14 '20 at 00:21
  • 1
    This solution did work for me running git version 2.25.0 on a Mac. – Marcos Buarque Jul 04 '20 at 20:31
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.

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
ynotjs
  • 17
  • 1
-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!

Akin Hwan
  • 607
  • 1
  • 9
  • 26
-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.

croppio.com
  • 1,823
  • 5
  • 28
  • 44
-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/*

Sid
  • 25
  • 4
-3

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245