Git Clone Error: Destination Path Already Exists Fix
Fix 'fatal: destination path "." already exists and is not an empty directory' Git clone error in current directory. Check hidden files with ls -a, use git init + remote add + fetch, or temp clone workaround for success.
How to clone a Git repository into the current directory when it reports ‘destination path “.” already exists and is not an empty directory’?
I’m running the following command:
git clone ssh://user@host.com/home/user/private/repos/project_hub.git ./
But I receive this error:
Fatal: destination path ‘.’ already exists and is not an empty directory.
The current directory appears empty (ls shows nothing). What am I missing, and how can I successfully clone the repository into the current directory?
The “git clone destination path already exists and is not an empty directory” error hits when you’re trying to clone a repo—like your git clone ssh://user@host.com/home/user/private/repos/project_hub.git ./ command—into the current directory (.), but Git detects something there already. Even if ls looks empty, hidden files (think .git, .DS_Store, or dotfiles) are probably lurking. Run ls -a to check, clean them out if needed, or better yet, initialize manually with git init, git remote add origin <url>, git fetch, and git checkout to pull the repo safely without nuking your folder.
Contents
- Understanding the Git Clone Error
- Why Your Directory Isn’t Truly Empty
- Quick Check: Spot Hidden Files
- Solution 1: Manual Git Init and Fetch
- Solution 2: Clone to Temp Folder and Move
- Other Workarounds and Pitfalls
- Sources
- Conclusion
Understanding the Git Clone Error
You’ve nailed the command—git clone <url> . targets the current directory. But Git’s protective: it refuses if the path (here, .) exists and isn’t empty. That exact message, “fatal: destination path ‘.’ already exists and is not an empty directory,” pops up because git clone expects a clean slate by default. It creates a new folder, grabs the repo, initializes .git, fetches history, and checks out the code—all in one go.
Why block it? Data loss prevention. Imagine overwriting local work accidentally. The official Git book spells this out: even invisible files trip it up. Your SSH URL looks solid too (ssh://user@host.com/...), so no auth issues here—just directory drama.
Common trigger? Folks assume ls means empty. Nope. Git peeks deeper.
Why Your Directory Isn’t Truly Empty
ls lies sometimes. It skips hidden files starting with .. Your current directory might have:
- A leftover
.gitfrom a failed init - macOS’s
.DS_Store(file metadata) - Linux temp files like
.fuse_hidden - Or custom dotfiles
According to the Git documentation, this safety check catches exactly that. One Stack Overflow thread nails it: users cloning into “empty” folders get burned by these ghosts. Graphite’s guide echoes this—git clone won’t touch non-empty spots to avoid surprises.
Quick reality check: ever cd into a fresh folder and still hit this? Hidden culprits.
Quick Check: Spot Hidden Files
Before fixes, diagnose. Fire up:
ls -la
Spot anything? .git means a repo’s half-baked already. .DS_Store? Mac artifact. Nuke 'em carefully:
rm -rf .git # If it's junk
rm -f .DS_Store # macOS cleanup
# Or broader: find . -name ".*" -type f -delete (use cautiously!)
Now retry git clone ssh://user@host.com/home/user/private/repos/project_hub.git .. Works? Great—edge case solved. But if you want files there (local edits?), skip to real solutions. Don’t rm -rf . blindly; that’s your whole folder.
Pro tip: ls -a is your new best friend for Git woes.
Solution 1: Manual Git Init and Fetch
This is the gold standard for cloning into current (possibly non-empty) directories. Git docs recommend it explicitly. Steps:
- Init the repo:
git init
- Link your remote (use your SSH URL):
git remote add origin ssh://user@host.com/home/user/private/repos/project_hub.git
- Grab everything:
git fetch origin
- Checkout the default branch (usually
mainormaster):
git checkout -t origin/main # Or replace 'main' with your branch
Boom—repo’s live in ., merged with existing files if any. A popular Gist breaks it down just like this, and Stack Overflow users swear by git pull origin master after as a shortcut (fetch + merge).
Handles submodules? Add git submodule update --init --recursive after.
Why prefer this? Preserves locals, no temp mess.
Solution 2: Clone to Temp Folder and Move
Need a full clone overwrite? Temp trick from community wisdom:
- Clone elsewhere:
git clone ssh://user@host.com/home/user/private/repos/project_hub.git temp_repo
- Backup your
.gitif exists (rare), then swap:
rm -rf .git # Careful!
mv temp_repo/.git .
rm -rf temp_repo
git reset --hard # Clean slate
Another Stack Overflow answer and this Gist detail it for “oops, started local first” scenarios. Quick, but riskier—back up first!
Other Workarounds and Pitfalls
- Non-empty with locals? After manual init/fetch, resolve conflicts via
git status. Edit, add, commit. - Submodules?
git submodule update --init. - Bare clone?
git clone --bare <url> .if no work tree needed. - Windows woes? Same, but watch paths.
- Pitfalls: Data loss on blind removes. SSH keys? Ensure
ssh-addor agent. Large repos?--depth 1for shallow.
Unix Stack Exchange warns of clashes—merge smartly. Reddit threads like this one push branch awareness first.
Pick based on needs: manual for safety, temp for speed.
Sources
- Git - Getting a Git Repository
- Git - git-clone Documentation
- Cloning a Git repository into an existing directory
- How do I clone into a non-empty directory?
- How to clone a git repo to an existing folder (not empty)
- git clone into non-empty directory
- Git: What’s the best practice to “git clone” into an existing folder?
- Git clone; how to deal with directory clashes?
Conclusion
Next time “git clone destination path already exists” blocks your current directory flow, start with ls -a, then lean on git init + git remote add + git fetch + git checkout for a clean win—it’s flexible and official. Temp clones work for quick resets, but watch for overwrites. You’ll clone like a pro, no more empty-folder illusions.