How to fix fatal: destination path 'repo' already exists and is not an empty directory in Git

GitBEGINNERLOWUpdated Dec 6, 2025ยท 555 views

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.

Quick fix

Fixes fatal: destination path 'repo' already exists and is not an empty directory

cd existing-directory
git status
git pull origin main
  1. Check if you actually need to clone
  2. Clone to a different directory name
  3. Remove the existing directory (if safe)
  4. Use git init and fetch into existing directory
git โ€” error
cd existing-directory
git status
git pull origin main
fatal: destination path 'repo' already exists and is not an empty directory

What this error means

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.

Typical symptoms

  • Running git clone fails immediately with 'fatal: destination path already exists'
  • The target directory name matches the repository name you are trying to clone
  • You previously cloned the same repository and are trying to clone again
  • A folder with the same name as the repo exists from a previous project or download
  • CI/CD pipelines fail during checkout step due to workspace not being clean

Common causes

  1. A directory with the same name as the repository already exists in the current location
  2. You previously cloned this repository and forgot about it
  3. Another project or downloaded archive created a folder with a conflicting name
  4. A failed or interrupted previous clone left behind a partial directory
  5. CI/CD workspace was not cleaned between runs
  6. You are trying to clone into your current directory using '.' but it contains files

How to fix "fatal: destination path 'repo' already exists and is not an empty directory"

1Check if you actually need to clone

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:

bash
cd existing-directory
git status
git pull origin main

If this works, you already have the repo and don't need to clone again.

2Clone to a different directory name

The simplest solution is to specify a different target directory name when cloning:

bash
git clone https://github.com/user/repo.git repo-new
# or
git clone https://github.com/user/repo.git my-custom-folder-name

This tells Git to create a new directory with your specified name instead of using the repository's default name.

3Remove the existing directory (if safe)

If you're certain you don't need the existing directory's contents, remove it first:

bash
# 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.git

Warning: The -rf flags force recursive deletion without confirmation. Make sure you have backups or don't need the files.

4Use git init and fetch into existing directory

If you have existing files you want to keep but also want to connect to a remote repository, use this approach:

bash
cd existing-directory
git init
git remote add origin https://github.com/user/repo.git
git fetch origin
git reset origin/main

This 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:

bash
git checkout -t origin/main
5Clone .git folder and move it (advanced)

For cases where you want to preserve existing files but fully adopt the remote repository's history:

bash
# 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 HEAD

Warning: git reset --hard HEAD will overwrite any local files that conflict with the repository. Back up important files first.

6Handle unrelated histories (if needed)

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):

bash
cd existing-directory
git init
git remote add origin https://github.com/user/repo.git
git fetch origin
git pull origin main --allow-unrelated-histories

The --allow-unrelated-histories flag permits merging two repositories that don't share a common ancestor commit. Resolve any merge conflicts that arise.

Advanced notes

### 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:

bash
git init
git remote add origin <url>
git fetch
git checkout -B main --track origin/main

This 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:

bash
git init
git remote add origin <url>
git config core.sparseCheckout true
echo "src/*" >> .git/info/sparse-checkout
git pull origin main

This clones only the src directory, avoiding conflicts with other existing folders.

Frequently asked questions

What does "fatal: destination path 'repo' already exists and is not an empty dire" mean?
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.
How do I fix "fatal: destination path 'repo' already exists and is not an empty dire"?
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.
What causes "fatal: destination path 'repo' already exists and is not an empty dire"?
A directory with the same name as the repository already exists in the current location You previously cloned this repository and forgot about it Another project or downloaded archive created a folder with a conflicting name
Is the "fatal: destination path 'repo' already exists and is not an empty dire" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading