This Git error occurs when you attempt to clone a repository into a directory that already exists and contains files. Git refuses to overwrite existing content to prevent accidental data loss.
When you run `git clone`, Git creates a new directory with the repository's name (or the name you specify) and downloads all repository contents into it. If a directory with that name already exists and contains any files—even hidden files like `.DS_Store` on macOS or a previous `.git` folder—Git will abort the clone operation with this fatal error. This is a safety feature. Git intentionally refuses to clone into a non-empty directory because doing so could overwrite or corrupt existing files. There is no `--force` flag to bypass this restriction; you must resolve the conflict manually by either removing the existing directory, choosing a different destination, or using an alternative approach to fetch the repository.
Before taking action, inspect what's in the directory causing the conflict:
ls -la repo/This shows all files including hidden ones. You may find:
- A .git folder from a previous clone
- Hidden system files like .DS_Store
- Actual project files you need to preserve
If the directory contains nothing important, remove it and clone again:
rm -rf repo
git clone https://github.com/user/repo.gitOn Windows:
rmdir /s /q repo
git clone https://github.com/user/repo.gitWarning: This permanently deletes the directory and all its contents. Make sure you don't need any files inside.
If you want to keep the existing directory, clone into a new location by specifying a different name:
git clone https://github.com/user/repo.git repo-newYou can name it anything you want. This is often the safest approach when you're unsure about the existing directory's contents.
If the existing directory is actually a Git repository you cloned before, you don't need to clone again—just update it:
cd repo
git pull origin mainCheck if it's a valid repository first:
cd repo
git statusIf this works, you already have the repo and just need to pull updates.
If you need to clone into a directory that has files you want to keep (like configuration files), use init and fetch instead:
cd existing-directory
git init
git remote add origin https://github.com/user/repo.git
git fetch origin
git checkout main -fNote: The -f flag forces checkout and may overwrite conflicting files. Review what's in the directory first.
In CI/CD environments, this error often occurs when the workspace isn't cleaned between runs. Solutions vary by platform:
GitHub Actions: The checkout action handles this automatically, but ensure you're using actions/checkout@v4
GitLab CI: Add cleanup or use GIT_STRATEGY: clone with GIT_CLEAN_FLAGS: -ffdx
Jenkins: Enable 'Clean before checkout' in your pipeline settings
Generic fix: Add a cleanup step before clone:
rm -rf ./repo || true
git clone https://github.com/user/repo.gitCloning into the current directory: If you want to clone directly into your current directory (represented by .), it must be completely empty—no files, no hidden files, nothing. The command git clone <url> . will fail if any file exists.
Why no --force flag?: Unlike some Git operations, git clone has no force option to overwrite existing directories. This is by design to prevent catastrophic data loss. The clone operation is meant for fresh checkouts only.
Sparse checkout alternative: If you only need part of a repository and want more control over what gets written, consider using sparse checkout:
git clone --filter=blob:none --sparse https://github.com/user/repo.git
cd repo
git sparse-checkout set path/to/directoryWorktrees: If you need multiple working copies of the same repository, use git worktree instead of cloning multiple times:
git worktree add ../repo-feature feature-branchwarning: 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