Skip to content

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:

  1. Different user contexts: Windows Git and Linux Git see different user identities
  2. Filesystem permissions: Windows tools accessing Linux files via \\wsl$ mounts don't properly handle Linux permission metadata
  3. IDE integration: Development tools often run Git with different privilege levels than your interactive shell

Solutions

Instead of using Windows Git to access WSL repositories, use the Linux Git installation:

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

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

  1. Open Windows File Explorer
  2. Navigate to your repository folder via \\wsl$\<distro-name>\path\to\repo
  3. Right-click the folder → Properties → Security → Advanced
  4. Click "Change" next to Owner
  5. Enter your Windows username and click "Check Names"
  6. Check "Replace owner on subcontainers and objects"
  7. Click OK to apply changes

Alternatively, use the command line:

cmd
takeown /r /d Y /f "path\to\repository"

Method 4: IDE-Specific Solutions

For IDEs like PHPStorm, PyCharm, or VSCode:

  1. Ensure your IDE uses the correct Git executable (Windows Git for Windows paths, WSL Git for WSL paths)
  2. Some IDEs may need elevated permissions or specific safe.directory configurations:
bash
# 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

  1. Consistent access method: Stick to either Windows tools or WSL tools for a given repository
  2. Regular updates: Keep both Windows Git and WSL Git updated to latest versions
  3. Backup configs: Export your Git configuration before making changes:
bash
git config --list > git_backup.config
  1. Test security settings: Periodically verify your safe.directory settings aren't exposing you to risks

When to Use Each Solution

ScenarioRecommended Solution
Occasional command-line useMethod 1 (wsl git)
IDE integration issuesMethod 4 (IDE-specific)
Repository copied from another systemMethod 3 (Fix ownership)
Multiple repositoriesMethod 2 (safe.directory) with specific paths

Troubleshooting

If solutions don't work:

  1. Verify your Git version: git --version
  2. Check current safe.directory settings: git config --global --get-all safe.directory
  3. Ensure you're running commands in the correct environment (Windows vs WSL)
  4. 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.