How to git clone into a non-empty directory

Photo by Yancy Min on Unsplash

How to git clone into a non-empty directory

Git is great for your own and team projects but what if you have a non-emtpy folder you want to clone into. This guide will help you to clone into it.

I’m building a few new projects. Some in just plain good old PHP and some with a nice framework like Slim 3. I’ve got a good working shared hosting solution at Vimexx and the only problem I have that when I git clone in a usually non empty directory I get these nice errors.

So here are the 5 simple steps to git clone in to that non empty folder!

You want to git clone in to that folder where there is already some files?

Let me guess. When you do:

git clone ssh://user@host.com/home/user/private/repos/project\_hub.git .

You get a:

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

So what are the options here?
If you do:

git help clone

You get:

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

No Shit! Sherlock!

So should you remove or move all the files and folders within the folder you want to clone into?

No!

Don’t worry! I’ve got you’re back!

The solution to Git Clone into a non empty folder is simple!

Solution to Git Clone into a non empty directory

The solution is very simple! Actually there are two solutions. Just see which one suits you!

git init      
git remote add origin PATH/TO/REPO      
git fetch      
git checkout -t origin/master

or

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

Git Clone

What is Git Clone? Git Clone, Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository, and creates and checks out an initial branch that is forked from the cloned repository’s currently active branch.

After the clone, a plain git fetch without arguments will update all the remote-tracking branches, and a git pull without arguments will in addition merge the remote master branch into the current master branch.

Executing the command git clone :whatever creates a directory in a current folder named whatever, and drops the contents of the git repo into that folder. Use a dot (.) behind the command to place the files directly into the current folder.

Did you find this article valuable?

Support Theo van der Sluijs by becoming a sponsor. Any amount is appreciated!