MEM
MEM

Reputation: 31387

How to get Git to clone into current directory

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 ?

Upvotes: 676

Views: 715383

Answers (22)

MEM
MEM

Reputation: 31387

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

Upvotes: 16

Nick Grealy
Nick Grealy

Reputation: 25942

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

Upvotes: 41

crazier-qi
crazier-qi

Reputation: 1

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

Upvotes: -4

Upendra
Upendra

Reputation: 703

use . at the end of your command like below

git clone URL .

Upvotes: 2

RexBarker
RexBarker

Reputation: 1801

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

Upvotes: 10

Catalin
Catalin

Reputation: 135

git clone ssh://user@host.com/home/user/private/repos/project_hub.git $(pwd)

Upvotes: 2

166_MMX
166_MMX

Reputation: 600

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

Upvotes: 9

croppio.com
croppio.com

Reputation: 1883

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.

Upvotes: -1

Akin Hwan
Akin Hwan

Reputation: 627

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!

Upvotes: -1

NSukonny
NSukonny

Reputation: 1088

Do

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

Directory must be empty

Upvotes: 70

igeocacher
igeocacher

Reputation: 129

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.

Upvotes: 10

John Josef
John Josef

Reputation: 324

shopt -s dotglob
git clone ssh://user@host.com/home/user/private/repos/project_hub.git tmp && mv tmp/* . && rm -rf tmp

Upvotes: 3

Sid
Sid

Reputation: 25

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/*

Upvotes: -2

ambes
ambes

Reputation: 5342

@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

Upvotes: 266

ynotjs
ynotjs

Reputation: 17

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.

Upvotes: 0

Roshan Pal
Roshan Pal

Reputation: 7806

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.

Upvotes: 715

Andre Holzner
Andre Holzner

Reputation: 18695

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.

Upvotes: 425

Jakehao
Jakehao

Reputation: 1019

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

Upvotes: 10

return1.at
return1.at

Reputation: 3140

git clone your-repo tmp && mv tmp/.git . && rm -rf tmp && git reset --hard

Upvotes: 58

phatblat
phatblat

Reputation: 4070

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.

Upvotes: 7

GoZoner
GoZoner

Reputation: 70235

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

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

Upvotes: 14

eckes
eckes

Reputation: 67177

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.

Upvotes: 63

Related Questions