Fix Git's "destination path already exists and is not an empty directory" clone error by removing or renaming the existing folder, or cloning into a new path.
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: refname 'feature' is ambiguous
How to fix 'warning: refname is ambiguous' in Git
error: The following untracked working tree files would be overwritten by checkout
Untracked working tree files would be overwritten by checkout
error: remote origin already exists
How to fix 'fatal: remote origin already exists' in Git
pathspec did not match any file(s) known to git
How to fix 'pathspec did not match any file(s) known to git' in Git
ssh: Could not resolve hostname github.com: Name or service not known
How to fix 'ssh: Could not resolve hostname github.com: Name or service not known' in Git