GrepBoard/Manuals
605 manuals ·

Fix git "detected dubious ownership in repository" error

·Tested on Ubuntu 24.04 (Docker) · git 2.43.0 · Alpine 3.22 · git 2.49.1 · 2026-10·Test scope ↓·6 steps

git says detected dubious ownership when the repo owner differs from the user running git. Fix the owner, or add just that path to safe.directory.

output
fatal: detected dubious ownership in repository at '/srv/app'To add an exception for this directory, call: 	git config --global --add safe.directory /srv/app

The repository directory is owned by a different user than the one running git, so git refused to even read its config. The check exists so that another user's repository config and hooks can't run with your privileges (the CVE-2022-24765 fix, in the 2.30.3 to 2.35.2 security releases). Fix ownership first; if you must add an exception, add only that path. Typical cases: working as root in a repository another user created, or root inside a Docker container running git on a bind-mounted folder owned by your host user.

[1] Check who is running git and who owns the repository.

console
$ id -unroot$ stat -c '%U %u %n' /srv/app /srv/app/.gitubuntu 1000 /srv/appubuntu 1000 /srv/app/.git

Here root opened a repository owned by UID 1000. Inside a container you see the host-side UID as is.

[2] Run git as the owner when you can. The same repository works with no extra config when its owner runs git.

console
$ su ubuntu -c 'cd /srv/app && git status --short; echo exit=$?'exit=0

For mounted sources in a container, start the container with --user "$(id -u):$(id -g)" so the file owner and the running user match. You can also hand the whole repository to the running user with chown -R — but if other people share that repository, they lose access, so check first. This was tested on a copy, /srv/app3:

console
$ chown -R root:root /srv/app3$ git -C /srv/app3 status --short; echo "exit=$?"exit=0

[3] If you can't change the owner, add only the path the error printed. --global writes to the running user's ~/.gitconfig.

console
$ git config --global --add safe.directory /srv/app$ git statusOn branch masternothing to commit, working tree clean$ git config --global --get-all safe.directory/srv/app

The exception must go into the global config of the user who runs git — root's ~/.gitconfig in the output above. In a container that config lives inside the container, so a fresh container needs it again.

[4] Don't put it in the repository's own .git/config — it is ignored. The docs say this setting is only read from protected configuration (system, global, command line), and that is what happens. If the global exception from [3] is still there it lets the command through, so the test below removes just that one entry first:

console
$ git config --global --unset --fixed-value safe.directory /srv/app$ git config --file /srv/app/.git/config safe.directory /srv/app$ git statusfatal: detected dubious ownership in repository at '/srv/app'To add an exception for this directory, call: 	git config --global --add safe.directory /srv/app

The point is that an untrusted repository can't declare itself safe.

[5] For many repositories under one folder, check your git version first. Allowing everything below a directory with dir/* appeared in the git 2.46 release notes; older versions silently ignore it.

console
$ git --versiongit version 2.43.0$ git config --global --add safe.directory "/srv/*"$ git -C /srv/app2 statusfatal: detected dubious ownership in repository at '/srv/app2'To add an exception for this directory, call: 	git config --global --add safe.directory /srv/app2

The same setting works on git 2.49.1:

console
$ git config --global --add safe.directory "/srv/*"$ git -C /srv/app2 status --short; echo "exit=$?"exit=0

Below 2.46, add each path separately.

[6] Use safe.directory '*' only as a last resort. As the docs put it, that value opts out of the check entirely; on shared servers or CI runners where others can create directories, the risk the check blocks comes right back. If you added it, remove just that value. --fixed-value makes git match * literally instead of as a regular expression:

console
$ git config --global --add safe.directory /srv/app$ git config --global --add safe.directory '*'$ git config --global --get-all safe.directory/srv/app*$ git config --global --unset --fixed-value safe.directory '*'$ git config --global --get-all safe.directory/srv/app

To empty the whole global list, use --unset-all. It also removes other paths you added on purpose, so look at the list with --get-all first:

console
$ git config --global --unset-all safe.directory; git config --global --get-all safe.directory; echo "exit=$?"exit=1

No output and exit code 1 means nothing is left in the global config. This only touches the global file (~/.gitconfig): if * sits in the system config (/etc/gitconfig), the check stays off. git config --show-origin --get-all safe.directory shows where each entry comes from. If you can't edit the system file, do what the docs say — add an empty value to reset the list read so far, then add back only the paths you trust:

console
$ git config --system --add safe.directory '*'$ git -C /srv/app2 status --short; echo exit=$?exit=0$ git config --global --add safe.directory ''$ git config --global --add safe.directory /srv/app$ git -C /srv/app2 status --short; echo exit=$?fatal: detected dubious ownership in repository at '/srv/app2'To add an exception for this directory, call: 	git config --global --add safe.directory /srv/app2exit=128$ git -C /srv/app status --short; echo exit=$?exit=0$ git config --show-origin --get-all safe.directoryfile:/etc/gitconfig	*file:/root/.gitconfig	file:/root/.gitconfig	/srv/appfile:.git/config	/srv/app

Only /srv/app, added after the empty value, passes; the system * no longer has any effect. (The .git/config entry on the last line is the one from [4], and it is not read.)

Official sources: git-config safe.directory · git 2.30.3 release notes (CVE-2022-24765) · git 2.46.0 release notes

Check it with the chmod · umask calculator tool ↗ — calculated in your browser.

#git#safe-directory#permissions#docker