Git clone refuses to write into an existing non-empty directory to prevent data loss. You can either remove the existing folder, clone to a different location, or use git init with remote add as an alternative approach.
This error occurs when you attempt to run `git clone` and the target directory already exists and contains files or subdirectories. Git intentionally refuses to clone into a non-empty directory as a safety measure to prevent accidentally overwriting your existing files. By default, `git clone <url>` creates a new directory named after the repository. If you run `git clone https://github.com/user/repo.git`, Git tries to create a folder called `repo` in your current working directory. If that folder already exists and is not empty, Git stops with this fatal error. This is a protective behavior, not a bug. Git cannot know whether the existing files are important work you want to keep or old files you want to replace. Rather than risk data loss, Git asks you to resolve the conflict manually.
Before removing anything, verify whether you already have the repository cloned. If the existing directory is already a valid Git repository, you may just need to pull updates instead:
cd existing-directory
git status
git pull origin mainIf this works, you already have the repo and don't need to clone again.
The simplest solution is to specify a different target directory name when cloning:
git clone https://github.com/user/repo.git repo-new
# or
git clone https://github.com/user/repo.git my-custom-folder-nameThis tells Git to create a new directory with your specified name instead of using the repository's default name.
If you're certain you don't need the existing directory's contents, remove it first:
# Check what's in the directory first
ls -la repo/
# If safe to delete, remove it
rm -rf repo
# Now clone
git clone https://github.com/user/repo.gitWarning: The -rf flags force recursive deletion without confirmation. Make sure you have backups or don't need the files.
If you have existing files you want to keep but also want to connect to a remote repository, use this approach:
cd existing-directory
git init
git remote add origin https://github.com/user/repo.git
git fetch origin
git reset origin/mainThis initializes Git in your existing directory, connects to the remote, and aligns your local state with the remote branch. Your existing files will show as modifications compared to the remote.
To set up branch tracking:
git checkout -t origin/mainFor cases where you want to preserve existing files but fully adopt the remote repository's history:
# Clone to a temporary directory
git clone --no-checkout https://github.com/user/repo.git temp-clone
# Move the .git folder to your existing directory
mv temp-clone/.git existing-directory/.git
# Remove temporary directory
rm -rf temp-clone
# Go to your directory and reset
cd existing-directory
git reset --hard HEADWarning: git reset --hard HEAD will overwrite any local files that conflict with the repository. Back up important files first.
If you initialized a repository locally and want to merge with a remote that has its own history (common when the remote has a README or LICENSE):
cd existing-directory
git init
git remote add origin https://github.com/user/repo.git
git fetch origin
git pull origin main --allow-unrelated-historiesThe --allow-unrelated-histories flag permits merging two repositories that don't share a common ancestor commit. Resolve any merge conflicts that arise.
### CI/CD Considerations
In CI/CD pipelines, this error often occurs when the workspace is not cleaned between builds. Most CI systems provide options to clean the workspace:
- GitHub Actions: Use actions/checkout@v4 with clean: true
- GitLab CI: Set GIT_STRATEGY: clone instead of fetch
- Jenkins: Enable "Delete workspace before build starts"
### The git init + remote add Pattern
The git init followed by git remote add pattern is the recommended alternative to cloning into existing directories:
git init
git remote add origin <url>
git fetch
git checkout -B main --track origin/mainThis gives you full control over which files to keep versus overwrite.
### Using git reset --mixed vs --hard
When aligning with a remote after git fetch:
- git reset --mixed origin/main: Keeps your local file changes (shows them as unstaged)
- git reset --hard origin/main: Discards all local changes to match remote exactly
Use --mixed when you want to review differences; use --hard when you want a clean slate.
### Sparse Checkout for Partial Clones
If you only need specific directories from a large repository:
git init
git remote add origin <url>
git config core.sparseCheckout true
echo "src/*" >> .git/info/sparse-checkout
git pull origin mainThis clones only the src directory, avoiding conflicts with other existing folders.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server