Git stops with "detected dubious ownership" in a folder you own
fatal: detected dubious ownership in repository at '/srv/app'Short answer
Git refuses to work in a repository whose folder belongs to a different user than the one running git. That happens after sudo, in shared volumes, in containers and in CI. Either make the folder yours with chown, or tell git to trust the path with safe.directory in your global config.
Fix it fast
1Find out which cause you have
Compare the folder owner with the user git runs as. If they differ, one of the causes below applies. The message itself names the repository path.
id -un ls -ld /srv/app /srv/app/.git2Match what you see, then jump to its fix
- 1
ls -ld shows an owner that is not the account you run git as (often root, or a deploy user).The folder belongs to another userFix - 2
The repository is deliberately owned by someone else: a shared checkout, a bind mount in a container, a CI workspace.A shared or mounted repository that must keep its ownerFix - 3
You added safe.directory but git still prints the same error.The safe.directory line sits in the wrong config fileFix
- 1
What does the error look like?
fatal: detected dubious ownership in repository at '/srv/app'
Cause 1 of 3
The folder belongs to another user
1Check
Git compares the owner of the repository with the current user and refuses on a mismatch. Typical sources are a clone made with sudo, files copied in by root, or a volume mounted from another machine or container.
id -un
stat -c '%U' /srv/app/.git2Fix
If the repository should be yours, change the owner. This is the cleanest fix because every other tool then agrees too.
sudo chown -R "$(id -un)":"$(id -gn)" /srv/app3Verify
git works again in the folder.
cd /srv/app && git statusConfirmed by git-scm.com
Cause 2 of 3
A shared or mounted repository that must keep its owner
1Check
Look at the list of paths git already trusts. An empty list, or one without this path, explains the refusal.
git config --global --get-all safe.directory2Fix
Add the path as a safe directory in your global (user) config. A value of * turns the check off for every path, and a path ending in /* trusts every repository below it. Use * only on a throwaway machine such as a CI runner.
git config --global --add safe.directory /srv/app
# all repositories under one folder
git config --global --add safe.directory '/srv/repos/*'3Verify
The path is listed and git commands run.
git config --global --get-all safe.directory
git -C /srv/app statusConfirmed by raw.githubusercontent.com
Cause 3 of 3
The safe.directory line sits in the wrong config file
1Check
Git only respects safe.directory from the system config, the global config and the command line. A line in the repository's own .git/config is ignored on purpose, because an untrusted repository must not be able to trust itself.
git config --show-origin --get-all safe.directory2Fix
Remove the line from the repository config and set it with --global (your user) or --system (the whole machine). For a single command you can pass it with -c.
git -C /srv/app config --local --unset-all safe.directory
git config --global --add safe.directory /srv/app
# one command only
git -c safe.directory=/srv/app -C /srv/app status3Verify
The origin column shows a file under your home folder or /etc, and git runs.
git config --show-origin --get-all safe.directoryConfirmed by git-scm.com
How do I stop it coming back?
- Clone and pull as the account that runs the app instead of using sudo git.
- Give deploy folders one owner and run deploy commands as that user.
- In containers, run as the user that owns the mounted code or set safe.directory in the image's global config.
- Avoid the * value outside disposable machines.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Servers
dnf cannot install MySQL on Amazon Linux 2023
dnf cannot find a mysql-server package on Amazon Linux 2023
Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.
3 causes, each with a fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fix