Skip to content

Git Windows Invalid Path Error

When working with Git repositories on Windows, you may encounter "invalid path" errors during cloning or checkout operations. This common issue typically stems from filename or pathname characters that are legal on Linux and macOS but prohibited in Windows file systems.

Problem Overview

The most frequent cause of this error is the presence of special characters in filenames that Windows considers invalid:

error: invalid path 'configs/perl-modules/DIST.64/perl-HTML-Tree-1:5.03-1.el6.noarch.rpm'

Windows prohibits these characters in filenames and paths:

  • \ (backslash)
  • / (forward slash)
  • : (colon)
  • * (asterisk)
  • ? (question mark)
  • " (double quote)
  • < (less than)
  • > (greater than)
  • | (pipe)

Additionally, Windows reserves certain filenames that cannot be used, such as:

  • CON, PRN, AUX, NUL
  • COM1 through COM9
  • LPT1 through LPT9

WARNING

These restrictions apply to NTFS file systems, which are standard on Windows. Even when using Git Bash or other Unix-like environments on Windows, the underlying file system still enforces these limitations.

Solutions

1. Disable NTFS Protection (Quick Fix)

The most effective temporary solution is to disable Git's NTFS protection mechanism:

bash
git config core.protectNTFS false

For a global setting that applies to all repositories:

bash
git config --global core.protectNTFS false

After setting this configuration, attempt your Git operation again:

bash
git checkout <branch-name>

Warning

Disabling NTFS protection may result in files with invalid characters being created with truncated names or zero content. Use this as a temporary workaround rather than a permanent solution.

2. Combined Sparse Checkout Approach

For repositories with many problematic files, combine sparse checkout with the NTFS setting:

bash
# Clone without checking out files
git clone --sparse -c core.protectNTFS=false -n <repository-url>

# Navigate to repository
cd <repository-directory>

# Configure sparse checkout
git config core.sparsecheckout true

# Add patterns to exclude problematic paths
echo "/*" >> .git/info/sparse-checkout
echo "!/configs/perl-modules" >> .git/info/sparse-checkout
echo "!/configs/perlbrew/perls/perl-5.24.1/man/man3" >> .git/info/sparse-checkout

# Complete the checkout
git checkout <branch-name>

3. Using Windows Subsystem for Linux (WSL)

For a more robust solution, use WSL to handle Git operations:

bash
# Install WSL if not already available
wsl --install

# Clone repository through WSL
git clone <repository-url>

WSL handles Windows filename restrictions transparently while allowing you to work with the files in Windows applications.

4. Manual File Correction

If you have access to modify the repository, identify and rename problematic files:

bash
# Check for files with invalid characters
git ls-tree -r HEAD | grep -E '[\\/:*?"<>|]'

Once identified, rename the files using Git:

bash
git mv "old:name.txt" "old-name.txt"
git commit -m "Rename file with invalid characters for Windows"
git push

5. Download ZIP Alternative

For one-time access to repository contents without fixing the underlying issue:

  1. Navigate to the repository on GitHub
  2. Click the "Code" button
  3. Select "Download ZIP"
  4. Extract the archive in Windows (most extraction tools will handle invalid characters gracefully)

Advanced Scenarios

Restoring Truncated Files

If you've already used core.protectNTFS false and ended up with zero-byte files:

bash
# Generate patch from a known good commit
git diff <good-commit-hash> > restoration.patch

# Apply patch in reverse to restore content
patch -R -f -i restoration.patch

# Clean up and commit
git add .
git commit -m "Restore files truncated due to invalid paths"

Handling Reserved Filenames

For repositories containing Windows-reserved names like aux.go or con.txt:

bash
# Check for reserved names (run in PowerShell)
Get-ChildItem -Recurse | Where-Object {$_.Name -match "^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\..*)?$"}

# Rename problematic files
git mv aux.go auxiliary.go
git commit -m "Rename reserved Windows filename"

Prevention Best Practices

To avoid these issues in cross-platform projects:

  1. Establish naming conventions that comply with Windows restrictions
  2. Add pre-commit hooks to validate filenames:
bash
#!/bin/sh
# .git/hooks/pre-commit
for file in $(git diff --cached --name-only); do
  if echo "$file" | grep -q '[\\/:*?"<>|]'; then
    echo "Error: Invalid character in filename: $file"
    exit 1
  fi
done
  1. Use CI/CD checks to validate repository compatibility across platforms
  2. Document filename restrictions in your contribution guidelines

When to Use Which Solution

Solution Selection Guide
ScenarioRecommended Approach
One-time cloneDownload ZIP or use WSL
Ongoing work on team repositoryFix filenames at source
Third-party repository you can't modifycore.protectNTFS false with sparse checkout
Development environmentWSL with Git
CI/CD pipelineLinux-based runners

Conclusion

The "invalid path" error in Git on Windows stems from filename compatibility issues between Unix-like systems and Windows NTFS restrictions. While temporary workarounds like disabling NTFS protection can help, the most sustainable solutions involve either using WSL, fixing problematic filenames at their source, or implementing preventive measures in your development workflow.

For teams working across platforms, establishing clear filename conventions and validation processes will prevent these issues from occurring in the first place, ensuring smooth collaboration regardless of operating system choices.