Yunfi

Yunfi

tg_channel
github
email

Windows Git repository dubious ownership ownership issues

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 USB drive or another system partition in a dual-boot 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 the ownership problem of this Git repository.

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:

  1. The user running the Git command is different from the user who created the folder at that time.

  2. The Git repository is located on a file system not managed by Windows, such as accessing a Git repository on a USB drive, in WSL, or on a drive of another system in a dual-boot setup using the Windows version of Git. (In most cases, this refers to a 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 continue reading to understand why I say this;
If the error 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 cause, the solution should be to change the owner of this repo.


However, Git does not store owner-related information in the .git folder, so how does the Windows version of Git capture this security issue?

In fact, Git checks the folder owner information in Windows. If it does not match the current user, it will start complaining. 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#

  1. Open the properties of the Git repository folder, click the "Security" tab, and then click "Advanced".

    Security Properties
  2. In the popped-up window, there is a column called "Owner" in the upper part. There is a "Change" button behind it. Click it.

  3. Fill in your username (the name of the User folder) in the input box, and then click "Check Names". It will automatically complete as MachineName/Username, for example, mine is CHRIS_ROG_M16\chris, and then click "OK".

  4. Go back to the previous interface, check the newly appeared "Replace owner on subcontainers and objects" checkbox, and then click "OK".

After completing all the steps, 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)

Owner Settings

References#

permissions - How to change ownership of a local GIT repository on Windows 11 - Stack Overflow

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.