Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

git "fatal: detected dubious ownership in repository at '/srv/app01/site'": check who owns the folder before you add safe.directory

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

When Git stops with fatal: detected dubious ownership in repository at '/srv/app01/site' and prints git config --global --add safe.directory /srv/app01/site, the folder is owned by a different account from the one running Git. Git refuses on purpose. Before you run the command it prints, check which account owns the folder and which account is running Git, then either fix the owner or name that one folder as safe.

What the error means

The Git 2.35.2 release notes say that release "merges up the fixes that appear in v2.30.3, v2.31.2, v2.32.1, v2.33.2 and v2.34.2 to address the security issue CVE-2022-24765", so the check is also in those maintenance releases. The git-config manual explains the rule: "By default, Git will refuse to even parse a Git config of a repository owned by someone else, let alone run its hooks, and this config setting allows users to specify exceptions". Our own reading: a repository owner could place configuration or hooks that run with your permissions.

In the Git 2.47.1 source, setup.c checks the owner of the working tree, the Git directory and, where present, the .git file. If any of them is not owned by the current user and the path is not in safe.directory, Git stops with the message in the title, then "To add an exception for this directory, call:" and the command. On Windows, compat/mingw.c also writes a line of the form '<path>' is owned by: with the owner, followed by but the current user is: and the account running Git.

The manual says safe.directory is "only respected in protected configuration", meaning the system, global and command scopes. An entry placed in the repository's own configuration is ignored. It adds that a process running as root under sudo is also allowed to use repositories owned by the account recorded in SUDO_UID.

Likely causes

Cause What you see How to confirm
Folder was created by another account (root, an installer, a colleague) Fails for a service or CI account, works for the owner The owner differs from the account running Git
A service runs Git as a different account from the interactive one Works in a shell, fails from the service id -un run as the service shows the service account
A container or network share maps users differently Our own reading: owner shows as a number or another account The owner printed for the folder is not the running account
Folder was copied or restored as another user Owner is the restoring account The owner matches the restore job
An entry exists, but in the wrong place or form Still fails after safe.directory was set The entry is in the repository config, or the path differs from the one in the message

Prove the cause before changing anything

All of these only read.

1. Take the path from the message. It is the folder Git named, for example /srv/app01/site. Note it exactly.

2. Show the owner of the working tree and its .git directory. The GNU coreutils manual page for stat gives %U as the user name of the owner, %G as the group name and %n as the file name.

stat -c '%U:%G %n' /srv/app01/site /srv/app01/site/.git

3. Show the account that runs Git, in the same session the failure came from. The id manual page lists -u for the user and -n for the name.

id -un

If the two differ, you have found the cause. On Windows, Git prints both accounts itself, in the is owned by: lines.

4. List what Git already trusts. The git-config manual lists --show-origin among the display options of git config list; any safe.directory lines it prints come with the file they were read from.

git config list --show-origin

Run it as the account that fails, because that account's configuration is the one that matters. The output lists every setting, so our own advice is to remove any credentials before pasting it into a ticket or onto a shared screen.

Our own reading: look for safe.directory entries, and note that one in a repository's own .git/config has no effect.

Fix

Choose by what step 3 showed. The cleanest fix is for the folder to belong to the account that runs Git.

Make the folder belong to the account that runs Git

Use this when the account is meant to work in this repository. The checks cover the top folder and its .git directory, so these two paths are the ones to change. Our own reading: files inside a repository owned by another account can still cause permission errors later.

Run the save and the back-out as the same account, because ~ is the home folder of whoever runs the command. Our own reading: chown normally needs administrator rights to give a folder to another account, so that account is usually an administrator. First save the current state, into a file outside the repository:

stat -c '%U:%G %n' /srv/app01/site /srv/app01/site/.git > ~/owner-before.txt

Risk: the next command changes the owner of two folders. Another service that relied on the old owner, such as a web server that writes into the folder, may lose access. The chown manual describes changing "the user and/or group ownership of each given file".

chown deploy /srv/app01/site /srv/app01/site/.git

Only if you need to back out, the saved file names the previous owner and group of each path. The chown above changed only the owner, so the group is as saved. Each saved line reads owner:group path, in the order %U:%G %n, so split it at the first space.

Risk: the next command changes the owner of the same two folders back, so the account that runs Git loses the ownership it gained and the check fails again for it. Run it once for each line of ~/owner-before.txt, with that line's owner and group in place of <owner>:<group> and its path in place of <path>.

chown <owner>:<group> <path>

Name this one folder as safe

Use this when the folder must stay owned by someone else and you trust that owner. The manual says safe.directory is a multi-valued setting, so you can add more than one directory with git config --add. Git's own message gives the exact command.

Run the save, the change and the undo below as the account that runs Git (the one id -un printed in step 3), because --global writes that account's own file. Our own reading: a service account often has no ~/.gitconfig. The manual says --global writes $XDG_CONFIG_HOME/git/config where that file exists and ~/.gitconfig does not, so the steps below name the one entry instead of copying a file. The commands use the older option forms, which every Git release from 2.35.2 onward has. The manual says these modes "have been deprecated in favor of subcommands" and lists each with its replacement, so they are not the newest syntax. First save the current state: list the entries the global configuration already holds, and look in the saved file for the path:

git config --global --get-all safe.directory > ~/safe-before.txt

The manual lists --get-all as replaced by git config get [--value=<pattern>] --all <name>, and says get "Returns error code 1 if key is not present". Our own reading: an empty saved file therefore means no entry existed, and the same holds for --get-all. If /srv/app01/site is already listed in ~/safe-before.txt, skip the next command: it would only add a second copy, and there is nothing to undo.

Risk: the next command tells Git, for the account that runs it, to trust one folder. After it, Git will parse that folder's configuration and may run its hooks, which the manual gives as the reason for the check. Trust only a folder whose owner and contents you know.

git config --global --add safe.directory /srv/app01/site

Only if you need to back out, and only when the saved file did not list the path, the one entry that was added is removed.

Risk: the next command removes the global safe.directory entry that is exactly /srv/app01/site, so Git again refuses repositories in that folder that are owned by another account. Entries for other folders stay. The manual lists --unset-all <name> [<value-pattern>] as replaced by git config unset [--value=<pattern>] --all <name>, and says a pattern is an extended regular expression unless --fixed-value is given. Our own reading: the anchors ^ and $ in the command below make the pattern match only that exact path, not a longer path that begins with it.

git config --global --unset-all safe.directory '^/srv/app01/site$'

The manual also describes setting safe.directory to * as the way "To completely opt-out of this security check". Our own advice: do not use it on a server where several accounts can write to folders.

Example (illustrative): On build-agent1, the setup script ran as root and cloned the site into /srv/app01/site. The CI service runs as deploy. Its job ends with fatal: detected dubious ownership in repository at '/srv/app01/site'. stat -c '%U:%G %n' /srv/app01/site /srv/app01/site/.git prints root:root for both lines, and id -un run as the service prints deploy. The administrator saves the owners, then runs chown deploy /srv/app01/site /srv/app01/site/.git. Git's ownership check now passes for both paths. No safe.directory entry was needed. Our own reading: whether the job then writes everything it needs depends on who owns the files inside the folder, which this change did not touch.

Confirm the fix

  1. git status, run as the failing account in the folder, prints without the fatal: line.
  2. stat -c '%U:%G %n' /srv/app01/site /srv/app01/site/.git shows the intended owner.
  3. The job or service that failed gets past the fatal: line; check that it also runs to its end.

What to watch afterwards

  • A later checkout by root into the same folder can recreate the problem. Run the setup and the service as the same account.
  • Keep safe.directory entries to exact folders. The manual says that a value ending in /* allows all repositories below that folder.
  • If the manual's SUDO_UID rule matters to you, remember it applies to root under sudo only; it does not cover a service account.

Risks and limits

  • These commands have not been run or lab-tested.
  • Adding a safe.directory entry gives that folder's owner the chance to run configuration and hooks as you. The manual describes the check as protection against that.
  • chown can break other software that expects the old owner. Save the owners first, as above.
  • The manual lists git config --list as replaced by git config list; on an older Git, use git config --list --show-origin for step 4. The save and the back-out above already use the older forms --get-all and --unset-all, which the manual lists as replaced by the get and unset subcommands.
  • The message and the added command are a reading of Git 2.47.1; other releases may word them differently, and the manual page at git-scm.com is for a newer release than 2.47.1.

Sources

  • git-config — Git documentation, manual page for releases up to 2.56.1, no date shown, accessed 3 December 2026
  • Git v2.35.2 Release Notes — Git source repository, tag v2.47.1, no date shown, accessed 3 December 2026
  • setup.c — Git source repository, tag v2.47.1, no date shown, accessed 3 December 2026
  • mingw.c — Git source repository, tag v2.47.1, no date shown, accessed 3 December 2026
  • stat(1) — Linux manual page, GNU coreutils 9.11, April 2026, accessed 3 December 2026
  • id(1) — Linux manual page, GNU coreutils 9.11, April 2026, accessed 3 December 2026
  • chown(1) — Linux manual page, GNU coreutils 9.11, April 2026, accessed 3 December 2026