Git Reference ยท Configuration Options
safe.directory setting
Reference for safe.directory: why Git refuses repositories owned by another user, how to trust a path, the wildcard, and where the setting is honored.
safe.directory is a list of repository paths that Git will use even though they belong to a different user than the one running the command. Without it, Git stops with fatal: detected dubious ownership in repository. The check exists because a repository's configuration can name programs to run (hooks, filters, core.fsmonitor, core.pager and more). If a stranger could plant a repository in a directory you work in, simply running git status there would run their code.
The error appears most often with shared network drives, containers that mount a host folder, CI runners that check out as one user and build as another, and repositories restored from a backup under a different account.
git config --global --add safe.directory /srv/projects/shop git config --global --add safe.directory '*' # trust everything (use with care) git config --get-all safe.directory
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
The second example uses GIT_TEST_ASSUME_DIFFERENT_OWNER, an environment variable that Git's own test suite uses to simulate a foreign owner. It is not meant for everyday use; it only lets us show the error without a second user account.
Syntax
git config --global --add safe.directory <path> git config --global --add safe.directory '*' git config --global --add safe.directory '%(prefix)/<path>'
| Value | Meaning |
|---|---|
/absolute/path |
Trust the repository whose working tree (or .git directory) is exactly this path. |
/absolute/path/* |
Trust every repository directly below this directory (a trailing /* pattern, Git 2.46 and later). |
* |
Trust every repository. Disables the protection. |
%(prefix)/path |
Resolve the path relative to the runtime prefix of the Git installation (used by portable builds). |
| empty value | Clears all earlier values from lower-priority files. |
safe.directory is a multi-valued key: every --add appends another line, and a path matches if it appears in any of them.
Errors: the message says which user owns the directory, and which command to run. Copy the exact path it prints, because Git compares the real, resolved path and a symlink path will not match.
Usage notes
- Only trusted config files are read. The value is honored in system, global and command-line (
-c) configuration, and ignored in a repository's own.git/config. Otherwise a malicious repository could vouch for itself. Putting it in a project config therefore does nothing. - Ownership is what is checked, not read permissions. On Unix the owner of the repository directory must equal your user ID. Running under
sudois a classic source of the error:sudo git pullruns as root in a folder owned by you. Git has a special case forsudo(it accepts the original user fromSUDO_UID) but not for every setup. - Bare repositories are checked too, so a server-side repository owned by another account needs the same treatment.
- The wildcard
*is convenient in containers and CI, where everything is disposable. Avoid it on a personal workstation or shared server, since it removes the very protection the check provides. - Clean up after yourself. Entries accumulate, and a path you trusted years ago may now hold something else.
git config --global --get-all safe.directoryshows what you have approved. - Better fixes exist: change the owner with
chown, run Git as the owning user, or check out as the same user that builds. Usesafe.directorywhen changing ownership is not an option. - Version notes: the check was added in security releases (Git 2.35.2 and the matching maintenance releases) after CVE-2022-24765. Windows uses the account SID for the comparison, and the trailing
/*pattern arrived in 2.46.