fatal: detected dubious ownership in repository at 'path/to/your/repo'
If you have switched users or reinstalled the system on Windows, then when you reopen the git repository on the D drive; or open a Git repository on a U disk or another system partition in a dual system, you may encounter the dubious ownership
error. Although Git provides a solution to add the path to safe.directory
, is there a way to directly modify the ownership of this Git repository?
This article takes the repository mentioned in 记一个围棋程序的制作过程 as an example to record how to solve this Git repository ownership problem.
Problem Description#
Currently, it seems that this problem only occurs on Windows and requires a relatively new version of Git.
The triggering methods are very simple and there are two situations:
-
The user running the Git command is different from the user who created the folder at that time.
-
The Git repository is located on a file system not managed by Windows, such as accessing a Git repository on a U disk, in WSL, or on a drive of another system booted in dual boot. (In most cases, it refers to the repo located on a non-NTFS drive)
When either of the two conditions is met, an error message similar to the following will be triggered:
fatal: detected dubious ownership in repository at 'D:/Projects/StupidGo'
'D:/Projects/StupidGo' is owned by:
'S-1-5-21-0000000000-0000000000-25500068-1001'
but the current user is:
'S-1-5-21-0000000000-0000000000-1346710304-1001'
To add an exception for this directory, call:
git config --global --add safe.directory D:/Projects/StupidGo
The error message provides a solution, which is to add the directory to Git's safe.directory
property. If you do this, you can find the following two lines in the ~/.gitconfig
file:
[safe]
directory = D:/Projects/StupidGo
If you triggered the error due to the second reason, then this is the best solution. However, you can still read on to see why I say this;
If it is triggered due to the first reason, this does not solve the problem at its root: the reason for the Git error is that the folder was not created by the current user, but in fact, this Git repository is mine, I just changed my username. From the root, the solution should be to change the ownership of this repo.
However, Git does not store ownership-related information in the .git folder, so how does the Windows version of Git capture this security issue?
In fact, Git checks the folder ownership information of Windows. If it does not match the current user, it will start to complain. In file systems not managed by Windows, there is no "folder owner" attribute, so an error will also occur.
From this, it can be seen that the solution to this problem is not from Git, but from the Windows folder.
Solution#
-
Open the properties of the Git repository folder, click the "Security" tab, and then click "Advanced".
-
In the popped-up window, there is a column called "Owner" in the upper part. There is a "Change" button behind it. Click it.
-
Fill in your own username (the name of the User folder) in the input box, and then click "Check Names". It will automatically complete as
Machine Name/Username
, for example, mine isCHRIS_ROG_M16\chris
, and then click "OK". -
Go back to the previous interface, check the newly appeared "Replace owner on subcontainers and objects" checkbox, and then click "OK".
After completing all of the above, the owner should be the current user (if the current user is logged in with a Microsoft account, the name and email of the Microsoft account will be displayed)
References#
permissions - How to change ownership of a local GIT repository on Windows 11 - Stack Overflow