Git 'fatal: detected dubious ownership in repository' error and safe solution
Determine the cause of Git dubious ownership error; Correct the repository ownership or just add the directory you verified to the safe.directory list.
Go's“fatal: detected dubious ownership in repository”error occurs when the user sees an unexpected difference between the owner of the repository folder and the user running the Git command. This check helps prevent config or hook behavior in another user's prepared repository from being deemed trustworthy.
The fastest lookingsafe.directory '*'setting turns off protection for all repositories. First find out why the ownership is different; Only implement a narrow solution for the repository you truly trust.
1. Which user is running the command?
whoami
id
git rev-parse --show-toplevel
ls -ld /path/to/repository
Compare the owner of the repository directory and the active user on Linux and macOS. In Windows, go to the Security/Advanced section in file Properties or in PowerShell.Get-AclYou can review the output. CI runner, Docker volume, WSL orsudoFiles created with are common causes of this difference.
2. Fix ownership on personal storage
If the repository really belongs to you and was created with the wrong user, the cleanest solution is to give file ownership to the correct account:
sudo chown -R "$USER":"$(id -gn)" /path/to/repository
Double check the path before running the command. If the application files on the server must belong to the service user, it may not be right to convert the entire directory to the developer account. Determine the ownership model on shared or production folders with the system administrator.
3. Use safe.directory for trusted shared storage
If differing ownership is by design and you've verified the repository, just add that absolute path to the safelist:
git config --global --add safe.directory /path/to/repository
git config --global --get-all safe.directory
For the Windows path, use the absolute path that Git recognizes:
git config --global --add safe.directory "C:/Projects/my-app"
This setting is not written to the local config within the repository; It is evaluated in the protected scope, for example in the global config. Thus, an untrusted repository cannot declare itself safe.
4. Don't expand the scope unnecessarily
git config --global --add safe.directory '*'considers all repositories safe. Don't use it without knowing what it disables, even in a single-user ad-hoc environment. To remove the incorrectly added record:
git config --global --unset-all safe.directory /path/to/repository
If there is more than one record--get-allSee the list first with. You can also use settings added at the system level.git config --show-origin --get-all safe.directoryPlease review it with its source.
5. Permanent solution in Docker and CI environment
When the host folder is connected to the container as a volume, UID/GID values may not match. Instead of adding a wildcard trust at every run in the image, run the container with the appropriate user ID, match volume ownership in a controlled manner, or simply define the workspace path as safe.directory.
In the CI system, the working folder must belong to the runner account. Checking out at the root stage and running the next step with a low-privileged user creates an ownership difference. Maintaining the same user model in the checkout and build steps is more permanent than suppressing the bug in every pipeline.
Verify that the solution works
git status
git config --show-origin --get-all safe.directory
git statusshould now work and the trusted list should only contain the required path. If the repository is not yours, before running it.git/configReview executable behavior such as , hook files, and build scripts.
Short guide
Common mistake
Covering all paths without examining why file ownership changedsafe.directory '*'make the setting permanent.