When you run a Git command inside a repository, you may suddenly be stopped by this error:
fatal: detected dubious ownership in repository at '/path/to/repo'Git refuses to operate because it thinks the repository is owned by a different user than the one running the command. It is a security feature added in Git 2.35, and it shows up most often on shared machines, mounted volumes, Docker containers, CI runners, and Windows/WSL setups.
Quick fix
In most cases, the fastest fix is to tell Git that the repository is safe. Replace /path/to/repo with the path shown in your error message:
git config --global --add safe.directory /path/to/repoThis resolves the error immediately for a repository you trust. If you want to understand why it happened and fix it at the source (ownership, CI, Docker, Windows/WSL), read on.
Why this error happens
Git raises this error when it detects that:
- The repository owner does not match the current user, or
- The repository sits in a location Git considers untrusted.
This is the safe.directory check introduced in Git v2.35 to stop a malicious repository owned by another user from running arbitrary commands through Git config.
Common situations that trigger it
- You previously ran Git (or
git clone) withsudo, so files are owned byroot. - The repository lives outside your home directory (
/tmp,/mnt,/Volumes, an external drive). - Ownership belongs to another user, for example in a team-shared directory.
- You are working inside GitHub Codespaces, a CI runner, or a Docker container that mounts the repo.
- On Windows/WSL, the repository is on a drive or mount whose owner differs from your account.
Check the current ownership
On macOS or Linux, confirm who owns the repository:
ls -la /path/to/repoIf the owner is not your user, that mismatch is the cause.
Fix 1: Register the repository with safe.directory
This is the standard, recommended fix and the right choice whenever you cannot (or should not) change ownership, such as on Windows/WSL, mounted volumes, or CI:
git config --global --add safe.directory /path/to/repoEach repository that triggers the error needs its own line. To confirm what is already registered:
git config --global --get-all safe.directoryNote: you can trust every directory at once with a wildcard, but this is not recommended because it disables the security check globally:
git config --global --add safe.directory '*'On Windows and WSL
On Windows and WSL you usually cannot change ownership the way you can on macOS or Linux, so safe.directory is almost always the correct fix. Use the exact path from the error message (for WSL repos under /mnt/c, add that path).
Fix 2: Fix ownership at the source with chown
On macOS and Linux, if the mismatch came from running Git with sudo, the cleaner long-term fix is to return ownership to your user:
sudo chown -R $(whoami) /path/to/repoWhat each part does:
sudo: run with administrator privileges.chown -R: change ownership recursively for every file and folder.$(whoami): your current logged-in user.
After this, the owner matches your account and Git stops flagging the repository. (This does not apply to Windows/WSL, where you should use Fix 1 instead.)
Check the repository's .git/config
You can also add the safe directory at the repository level. Open .git/config inside the repository:
vim .git/configA typical file looks like this, and you can add a [safe] section:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[safe]
directory = /path/to/repoIn practice the git config --global --add safe.directory command above edits this file for you, so hand-editing is only needed when you want the setting to live with a specific repository.
CI runners, Docker, and mounted directories
In CI (GitHub Actions, GitLab CI) and Docker, the repository is usually checked out outside the home directory or mounted with a different user ID, which is why this error is so common there. Mark the working directory as safe as part of the job:
git config --global --add safe.directory "$PWD"For Docker, align the user ID or fix ownership so the mounted repository matches the container user:
# Run the container as your host user
docker run --user $(id -u):$(id -g) ...
# Or fix ownership inside the Dockerfile
RUN chown -R myuser:mygroup /workspace/repoBackground: the Git 2.35 security change
This behavior was introduced in Git v2.35.1 (2022) to block operations in directories whose owner does not match, or whose path is unsafe. Check your version with:
git --versionIf you manage Git with Homebrew on macOS, keep it current so behavior stays predictable:
brew upgrade gitSummary
The "fatal: detected dubious ownership in repository" error is a Git security check, not a bug in your repository. To clear it:
- Quick fix:
git config --global --add safe.directory /path/to/repo. - Root cause on macOS/Linux: fix ownership with
chown -R $(whoami). - Windows/WSL: use
safe.directory(you usually cannot change ownership). - CI/Docker: add
safe.directory "$PWD"or align the container user ID. - Optionally add a
[safe]section to the repository's.git/config.
References
- Git 2.35.1 Security Release
- Git Documentation: safe.directory
- Stack Overflow: detected dubious ownership in repository