Blog / git
Git says "dubious ownership" when your coding agent runs it
Your agent runs git in a container or as another user and gets fatal: detected dubious ownership. Why git refuses, and the fixes that don't open a hole.
On this page
- What the error means
- Why agents hit it so often
- Fix 1: run the agent as the owner
- Fix 2: trust exactly this one folder
- The fix to avoid
- When the agent made it worse
- Where Joxo fits
- Frequently asked questions
- What does "detected dubious ownership in repository" mean?
- How do I fix dubious ownership in Docker?
- Can I put safe.directory in the repository's .git/config?
- Is safe.directory '*' safe?
- Why does my coding agent's git work in one folder and not another?
- Try it with your team
You put your coding agent in a container, a sandbox or a separate user account so it can't touch the rest of your machine. Then the first git status it runs fails, and so does every git command after it. The agent may copy the repository somewhere else, change git settings it shouldn't, or give up on git and edit files blind. The cause is one ownership check, and the fix is small once you know which side to change. We ran every command here with git 2.43 on test repositories.
What the error means#
fatal: detected dubious ownership in repository at '/work/app'
To add an exception for this directory, call:
git config --global --add safe.directory /work/appSince git 2.35.2, git refuses to use a repository whose top folder is owned by a different user than the one running git. The reason is security: a repository's .git/ can name programs git will run, such as hooks, an editor or a core.fsmonitor command. If anyone could drop a repository in a shared folder and have your git obey its config, they could run code as you.
The check looks at the owner of the working folder and its .git folder. If either belongs to someone else, git stops. In our test, a folder owned by nobody with a .git owned by the current user still failed.
Why agents hit it so often#
The usual setups all put a second user between you and the files:
- Docker with a bind mount. Your project is owned by your user (often uid 1000) and the container runs as root, or as a user with another uid. Inside the container, the files belong to "someone else".
- A dedicated agent user. You made an
agentaccount so the agent can't read your home folder, and it works in a checkout your user owns. - A cloud or CI runner. The checkout step ran as one user and the agent runs as another.
- sudo. The agent ran a git command with sudo, so root is now looking at your files.
Check who owns the folder and who is asking:
stat -c '%U %u' /work/app
id -un; id -uIf the two don't match, that's the whole problem.
Fix 1: run the agent as the owner#
The cleanest fix is to make the user running git the same user that owns the files. For Docker, pass your uid and gid:
docker run --user "$(id -u):$(id -g)" -v "$PWD":/work/app -w /work/app agent-imageThis also fixes the opposite problem. When an agent runs as root in a container, every file it creates on the bind mount is owned by root, and you later get "permission denied" when you edit or delete them on the host. Running as your own uid avoids both.
If the agent already left root-owned files behind, give them back:
sudo chown -R "$(id -u):$(id -g)" /work/appCheck git status afterwards; nothing in the repository changes, only who owns the files.
Fix 2: trust exactly this one folder#
When the agent has to run as another user, tell git that this one repository is fine. Run this as the user that runs git, inside the container if that's where the agent is:
git config --global --add safe.directory /work/appA few details we checked:
- It must be global, system or on the command line. Git ignores
safe.directoryin the repository's own.git/, since that's the file it doesn't trust. Setting it there still failed in our test.config - The path must match exactly.
/work/with a trailing slash didn't matchapp/ /work/. Copy the path from the error message.app - Per command works too.
git -c safe.directory=/worked, which is handy in a script or a one-off command.work/ app status - Each user has its own global config. Adding it on your host does nothing for root inside a container. Put it in the image, for example with a
RUN git config --system --add safe.directory /line in the Dockerfile.work/ app
The fix to avoid#
You'll find this suggested a lot:
git config --global --add safe.directory '*'It works, and it turns the check off for every repository that user ever opens. For a throwaway container that only ever sees your own checkout, the risk is small. On a shared machine, a runner that clones other people's code, or your own laptop, it brings back exactly what the check prevents: a repository someone else controls running commands as you. Name the folders instead.
Also watch for your agent adding the wildcard by itself. It reads the error, sees the hint, and the next "fix" it tries may well be the star. Tell it in CLAUDE.md or AGENTS.md to stop and tell you when git reports dubious ownership, instead of changing global git config.
When the agent made it worse#
If an agent hit the error and kept going, check what it did:
- It copied the repository. Some agents clone or copy the project to a folder they own and carry on there. Your real checkout then has none of the changes. Look for a second copy in
/tmpor the agent's home folder, and bring the commits back withgit fetch /and a merge or cherry-pick; see take one commit from an agent's branch.path/ to/ copy - It ran
git initagain. In a folder that already has a.git,git initis harmless. In a parent or child folder, it creates a second repository; see an agent cloned a repo inside yours for how to clean up a nested one. - It changed ownership. A
chown -Rto the agent's user fixes the agent's git and breaks yours, with the same error on your side. Hand the files back with the chown above. - It changed global config. Look at
git config --global --list --show-originfor newsafe.directorylines, and remove any you didn't mean withgit config --global --unset-all safe.directory, then add back the ones you want. Our guide to git settings your agent changed covers the rest.
Where Joxo fits#
When agents run in containers and sandboxes on several teammates' machines, a stuck agent is easy to miss for an hour. In Joxo, an agent that hits a wall like this can say so in the project, and you see it from the desktop app or your phone. Setup is one line pasted into your agent.
Frequently asked questions#
What does "detected dubious ownership in repository" mean?#
The repository folder is owned by a different user than the one running git. Since git 2.35.2, git refuses to read such a repository, because its config could run programs as you.
How do I fix dubious ownership in Docker?#
Run the container as the user that owns the files, with docker run --user "$(id -u):$(id -g)". If it must run as another user, add the folder with git config --system --add safe.directory in the image.
Can I put safe.directory in the repository's .git/config?#
No. Git only reads safe.directory from system or global config, or from -c on the command line. A value in the repository's own config is ignored.
Is safe.directory '*' safe?#
It disables the check for every repository that user opens. That's acceptable in a throwaway container with only your own code, and risky anywhere else. Name each folder instead.
Why does my coding agent's git work in one folder and not another?#
The check is per repository. The folders that fail are owned by a different user than the one the agent runs as, often because a container or sudo created them.