Feb 14, 2020

Git clone into non-empty directory

If you need to bring a remote Git repository into a folder that already contains files, you can’t use git clone directly — it requires an empty directory. Here’s the workaround.

Steps

Step 1: Navigate to your existing directory

1
cd nonemptydir

Step 2: Initialize a new Git repo

1
git init

Step 3: Add the remote repository

1
git remote add origin https://path/to/the/repository.git

Step 4: Pull the remote history

The --allow-unrelated-histories flag is required because your local folder has no shared commit history with the remote.

1
git pull origin master --allow-unrelated-histories

Step 5: (Optional) View the repo in Sourcetree

1
stree .

Step 6: Stage, commit, and push

1
2
3
git add -A
git commit -m 'first commit'
git push -u origin master