Fixing Git "detected dubious ownership in repository" Error in WSL
Problem
When using Git with Windows Subsystem for Linux (WSL), you may encounter the error:
fatal: detected dubious ownership in repository at '/path/to/repository'
This typically occurs when accessing Linux-side repositories from Windows tools (like Git Bash, IDEs, or PowerShell) after a recent Git update.
Root Cause
Starting with Git versions 2.35.2 and 2.36, Git introduced stricter repository ownership checks as a security measure. This security feature prevents potential attacks where malicious actors could manipulate Git operations by placing repositories in directories they don't own.
In WSL environments, this becomes problematic because:
- Different user contexts: Windows Git and Linux Git see different user identities
- Filesystem permissions: Windows tools accessing Linux files via
\\wsl$
mounts don't properly handle Linux permission metadata - IDE integration: Development tools often run Git with different privilege levels than your interactive shell
Solutions
Method 1: Use Linux Git from Windows (Recommended)
Instead of using Windows Git to access WSL repositories, use the Linux Git installation:
# From Git Bash or Windows terminal
wsl git status
wsl git add .
wsl git commit -m "message"
This approach maintains security while ensuring proper permission handling.
Method 2: Configure safe.directory (Balanced Approach)
Add your WSL directories to Git's safe directory list:
# From Windows Command Prompt or PowerShell
git config --global --add safe.directory "//wsl$/Ubuntu/path/to/repo"
# For multiple directories or all directories
git config --global --add safe.directory "*"
WARNING
Using safe.directory "*"
disables the security check globally. Only use this if you understand the risks and trust all repositories on your system.
Method 3: Fix File Ownership (Permanent Solution)
If the repository was copied from another system or created with different permissions:
- Open Windows File Explorer
- Navigate to your repository folder via
\\wsl$\<distro-name>\path\to\repo
- Right-click the folder → Properties → Security → Advanced
- Click "Change" next to Owner
- Enter your Windows username and click "Check Names"
- Check "Replace owner on subcontainers and objects"
- Click OK to apply changes
Alternatively, use the command line:
takeown /r /d Y /f "path\to\repository"
Method 4: IDE-Specific Solutions
For IDEs like PHPStorm, PyCharm, or VSCode:
- Ensure your IDE uses the correct Git executable (Windows Git for Windows paths, WSL Git for WSL paths)
- Some IDEs may need elevated permissions or specific safe.directory configurations:
# For IDE tools that run as different users
sudo git config --global --add safe.directory /path/to/repo
Method 5: Use Proper Path References
Ensure you're accessing WSL repositories through the proper \\wsl$
path rather than drive letters:
- Incorrect:
U:\home\project\
(mounted drive) - Correct:
\\wsl$\Ubuntu\home\project\
(proper WSL path)
Best Practices
- Consistent access method: Stick to either Windows tools or WSL tools for a given repository
- Regular updates: Keep both Windows Git and WSL Git updated to latest versions
- Backup configs: Export your Git configuration before making changes:
git config --list > git_backup.config
- Test security settings: Periodically verify your safe.directory settings aren't exposing you to risks
When to Use Each Solution
Scenario | Recommended Solution |
---|---|
Occasional command-line use | Method 1 (wsl git) |
IDE integration issues | Method 4 (IDE-specific) |
Repository copied from another system | Method 3 (Fix ownership) |
Multiple repositories | Method 2 (safe.directory) with specific paths |
Troubleshooting
If solutions don't work:
- Verify your Git version:
git --version
- Check current safe.directory settings:
git config --global --get-all safe.directory
- Ensure you're running commands in the correct environment (Windows vs WSL)
- Try the command with administrator privileges if ownership issues persist
INFO
The security check exists for good reason. Only bypass it when you're certain about the repository's safety and ownership.
By understanding the underlying permission systems and applying the appropriate solution, you can resolve the "dubious ownership" error while maintaining a secure development environment.