The 'Sparse checkout leaves no file in the working directory' error occurs when Git's sparse-checkout patterns don't match any files in the repository. This is commonly caused by incorrect path specifications, file encoding issues on Windows, or attempting to checkout non-existent directories.
This error indicates that after applying your sparse-checkout configuration, Git cannot find any files that match the specified patterns. When using sparse checkout, Git limits the working tree to only include files that match certain patterns or directories. If none of your patterns match actual content in the repository, Git refuses to proceed because you would end up with an empty working directory. Sparse checkout is a powerful feature for working with monorepos or large repositories where you only need a subset of the files. In **cone mode** (the default in Git 2.25+), you specify directories to include, and Git automatically includes all parent directories and files. In **non-cone mode**, you use gitignore-style patterns for more granular control. The error typically occurs in these scenarios: 1. The sparse-checkout file contains paths that don't exist in the repository 2. The sparse-checkout file has encoding issues (especially on Windows) 3. You cloned with `--no-checkout` and the index is empty when sparse patterns are applied 4. There's a mismatch between what you think exists in the repo and what actually exists
First, confirm that the path you're trying to sparse-checkout actually exists in the repository.
# List all directories in the repository at the root
git ls-tree --name-only -d HEAD
# List directories at a specific path
git ls-tree --name-only -d HEAD path/to/
# Search for a specific directory name
git ls-tree -r --name-only HEAD | grep -i "your-directory-name"
# Check repository structure without full checkout
git ls-tree --name-only HEADImportant: The folder structure shown in GitHub/GitLab web interface might not match the actual repository structure. Always verify paths using git ls-tree.
On Windows, PowerShell and CMD create files with UCS-2 LE BOM encoding, which Git cannot parse. The sparse-checkout file must be in UTF-8 or ASCII encoding.
Option 1: Use Git Bash instead of PowerShell/CMD
# In Git Bash, not PowerShell
git sparse-checkout set path/to/directoryOption 2: Force ASCII encoding in PowerShell
# Instead of echo, use Set-Content with ASCII encoding
Set-Content .git/info/sparse-checkout "path/to/directory" -Encoding Ascii
# Or use Out-File with explicit encoding
echo "path/to/directory" | Out-File -Encoding ASCII .git/info/sparse-checkoutOption 3: Manually create the file in a text editor
Open Notepad or VS Code, add your paths (one per line), and save as UTF-8 without BOM.
Ensure your sparse-checkout paths follow the correct format:
# Correct format: forward slashes, no trailing slash, no leading slash
git sparse-checkout set path/to/directory
# In cone mode, specify directories only (no glob patterns)
git sparse-checkout set apps/my-app libs/shared
# Check current sparse-checkout configuration
git sparse-checkout listPath rules:
- Use forward slashes (/) even on Windows
- No leading slash (use path/to/dir not /path/to/dir)
- No trailing slash or spaces
- Case must match the repository exactly on Linux/macOS
If you cloned with --no-checkout, the index is empty. You need to populate it before sparse checkout patterns can be applied.
# After clone --no-checkout, initialize sparse-checkout first
git sparse-checkout init --cone
# Then set your directories
git sparse-checkout set path/to/directory
# Or if that doesn't work, reset and checkout
git reset --hard HEAD
# Then re-apply sparse checkout
git sparse-checkout reapplyFor Git 2.25+, the recommended modern workflow is:
git clone <URL> --no-checkout <directory>
cd <directory>
git sparse-checkout init --cone
git sparse-checkout set apps/my-app libs/my-lib
git checkout mainIf your sparse checkout is in a broken state, disable it completely and start fresh:
# Disable sparse checkout and restore all files
git sparse-checkout disable
# Verify all files are restored
git status
# Start over with sparse checkout
git sparse-checkout init --cone
git sparse-checkout set your/actual/pathIf disable doesn't work, manually reset:
# Remove sparse checkout config
git config --unset core.sparseCheckout
git config --unset core.sparseCheckoutCone
# Remove sparse-checkout file
rm .git/info/sparse-checkout
# Reset to get all files back
git read-tree -mu HEADInspect the actual content and encoding of your sparse-checkout configuration:
# View sparse-checkout file content
cat .git/info/sparse-checkout
# Check file encoding (Linux/macOS)
file .git/info/sparse-checkout
# On Windows Git Bash, check for hidden characters
cat -A .git/info/sparse-checkout
# Compare against what git shows
git sparse-checkout listThe file should look like:
/*
!/*/
/path/to/directory/In cone mode, Git automatically adds the patterns. The /* keeps root files, !/*/ excludes all directories, and then specific paths are included.
### Cone Mode vs Non-Cone Mode
Git sparse-checkout has two modes with different pattern syntax:
Cone Mode (default in Git 2.25+):
- Specify directories only, not individual files
- Faster pattern matching using hash-based algorithms
- Automatically includes parent directories
- Recommended for most use cases
git sparse-checkout init --cone
git sparse-checkout set apps/frontend packages/sharedNon-Cone Mode:
- Uses gitignore-style patterns
- Can match individual files and use wildcards
- Slower for large repositories
git sparse-checkout init --no-cone
echo "*.md" >> .git/info/sparse-checkout
echo "src/**/*.ts" >> .git/info/sparse-checkout
git sparse-checkout reapply### Debugging Pattern Matching
# See which files would be checked out with verbose output
GIT_TRACE=1 git sparse-checkout reapply
# List all files that should be in the sparse checkout
git ls-files --sparse
# Check the sparse index (Git 2.32+)
git sparse-checkout list --sparse-index### CI/CD Considerations
In CI/CD pipelines, use the full workflow:
# GitHub Actions example
- uses: actions/checkout@v4
with:
sparse-checkout: |
apps/my-app
packages/shared
sparse-checkout-cone-mode: true### Common Mistakes
1. Mixing cone and non-cone syntax: Cone mode only accepts directory paths, not glob patterns
2. Assuming web UI matches git paths: Some repos have symlinks or unusual structures
3. Forgetting to checkout after set: In some workflows, you need an explicit git checkout after setting patterns
4. Empty repository branch: If you're on a branch with no commits matching the path, you'll get this error
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git