Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Git stops with "detected dubious ownership" in a folder you own

Errorfatal: 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.

  • Applies to Git 2.45 and newer, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/.git
  2. 2Match what you see, then jump to its fix

    1. 1ls -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. 2The 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. 3You added safe.directory but git still prints the same error.The safe.directory line sits in the wrong config fileFix

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/.git

2Fix

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/app

3Verify

git works again in the folder.

cd /srv/app && git status

Confirmed by git-scm.com

Did this fix it?

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.directory

2Fix

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 status

Confirmed by raw.githubusercontent.com

Did this fix it?

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.directory

2Fix

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 status

3Verify

The origin column shows a file under your home folder or /etc, and git runs.

git config --show-origin --get-all safe.directory

Confirmed by git-scm.com

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.