How to get Git to clone into current directory
Matthew Harrington
I'm doing:
git clone ssh://user@ ./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 ?
722 Answers
simply put a dot next to it
git clone :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.
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 masterIn my case, this produces a .git/config file which is equivalent to the one I get when doing a clone.
@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 Do
git clone .Directory must be empty
1To be sure that you could clone the repo, go to any temporary directory and clone the project there:
git clone ssh://user@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/locationThis is safe and doesn't require any magical stuff around.
2git clone your-repo tmp && mv tmp/.git . && rm -rf tmp && git reset --hard 1 Specifing the absolute current path using $(pwd) worked for me.
git clone $(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 .git version: 2.36.1
4If the current directory is empty, then this will work:
git clone <repository> foo; mv foo/* foo/.git* .; rmdir foo 2 The solution was using the dot,
so:
rm -rf .* && git clone ssh://user@ .`rm -rf .* && may be omitted if we are absolutely sure that the directory is empty.
Credits go to: @James McLaughlin on comments
5In addition to @StephaneDelcroix's answer, before using:
git clone git@ .make sure that your current dir is empty by using
ls -a 2 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:
- 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
- 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.
Further improving on @phatblat's answer:
git clone --no-checkout <repository> tmp \ && mv tmp/.git . \ && rmdir tmp \ && git checkout masteras one liner:
git clone --no-checkout <repository> tmp && mv tmp/.git . && rmdir tmp && git checkout master
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 temp
cp -r temp/* .
rm -rf tempHere, I used cp -r instead of mv, since that copies hidden files and directories. Then dispose of the temporary directory with rm -rf
Improving on @GoZoner's answer:
git clone <repository> foo; shopt -s dotglob nullglob; mv foo/* .; rmdir fooThe 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.
shopt -s dotglob
git clone ssh://user@ tmp && mv tmp/* . && rm -rf tmp 2 git clone ssh://user@ $(pwd) 2 use . at the end of your command like below
git clone URL . 2 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:
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 The above was my issue and simply elevating worked for me.
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!
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.
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/*
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