171

I used git for the last few years in this context:

  • Host = my laptop, windows.
  • WSL enabled
  • Repos live in the Linux side.
  • I access them both from the Linux and the Windows side.

I can access the files in Linux either via git-bash like this (via the //wsl$/ share):

Access linxux from git bash

Or natively in the WSL bash terminal:

Access Linux from wsl bash

Those accesses go to the very same directory.

Error

Now it happens that if I do git status inside a repo from the windows side it gives the error fatal: detected dubious ownership in repository at:

error in git-bash

While in the WSL-Linux it does not, for the same directory:

no error in wsl-bash

Since when?

It did not happen before. I've been using this setup for years. This started happening 2 days ago.

In fact, I installed a newer version of git-bash 2 days ago and I suspect the bash environment could condition this.

I work with about 100 repos, and I have found to fail in all of them which I've tried (about 10 repos). Expectedly it'll happen to those 100 repos.

None of those previously-working now-failing repos have been touched, so neither users, neither permissions have changed.

So mutating from "well" to "bad" is not in the filesystem side, must be in the git-bash side.

Problem

I don't want to just white-list it with safe.directory. I want to understand what's going on behind the scenes. Why it happens today and not 3 days ago. I want to know "what does git expect" and see how can I correct it.

Investigation so far

The users seem correct. From the linux side:

linux ids

And from the windows side it also matches the hard disk and the id:

windows ids

Question

How can I tell the ownership that is expected by git for it to do not complain?

1
  • 76
    Others may disagree, but I will say this is a well-organized post and bears recognition for that fact. It is easily scannable and expresses frustration without complaining. Good job Commented Oct 18, 2022 at 19:24

12 Answers 12

142

In fact, I installed a newer version of git-bash 2 days ago and I suspect the bash environment could condition this.

I understand you installed a new version of Git for Windows, which includes Git Bash. Newer versions of Git, starting with 2.35.2 and 2.36, including Git for Windows, are stricter about directory ownership: https://github.blog/2022-04-18-highlights-from-git-2-36/#stricter-repository-ownership-checks.

When you use git from Git Bash, you use the Windows program, even if you cd to the //wsl$/ mount. Git for Windows does not have any special code to deal with the permissions of the WSL mount, so that's why you get the error. You can't fix that without modifying the Git source code.

An alternative could be to use wsl git instead of git when in Git Bash, which would then use the Linux executable.

Or, as you wrote, just use git config --global safe.directory '*' to bypass that security feature if you do not consider yourself at risk. If you want to only bypass this warning without modifying your config, you can do it with -c as noted here: git -c safe-directory='*' <your sub-command here>

[Editor's Note: If you are using Windows Command Prompt, do not add single-quotes to the asterisk, i.e., git config --global safe.directory *.]

11
  • I see. Using wsl git will not be so trivial as my wsl is configured to start in certain difectories and the overall is to provide a CLI for the GUI (for example SourceTree). I would be happy to contribute to the git source code trying to overcome the //wsl$/ by exploring more, but since I don't have the environment to compile git and also we need to patch git in a way more restrictive way that the usual pull-requests, I am affraid of trying to contribute and ending in a sterile contribution... Any tips on contributing to the git source code without too much overhead? Commented Aug 25, 2022 at 14:36
  • I see here github.com/git/git/blame/… in line 1483 that the responsible is the result of setup_git_directory_gently_1() being GIT_DIR_INVALID_OWNERSHIP. In addition, the only places where it can be assigned inside the functions are lines 1329 and 1351 and both assignations depend on ensure_valid_ownership(), which is defined in line 1144 and in turn this one depends on is_path_owned_by_current_user(). which ends up being an alias here compat/mingw.h to is_path_owned_by_current_sid. Is it that we need to patch git? Or WSL? Commented Aug 25, 2022 at 14:57
  • 3
    I would first start by opening an issue at github.com/git-for-windows/git/issues, with a minimal reproducer, and see what comes out of the discussion. The Git for Windows maintainer is usually very helpful and should be able to guide you.
    – philb
    Commented Aug 25, 2022 at 15:58
  • 2
    Thanks! This was the solution to my problem git config --global safe.directory '*' Commented Oct 18, 2022 at 19:23
  • 6
    Key point in this answer which helped me is the Editor's Note: "If you are using Windows Command Prompt, do not add single-quotes to the asterisk, i.e., git config --global safe.directory *."
    – finch
    Commented Feb 8, 2023 at 16:46
37

I suddenly ran into this issue after reinstalling Windows due to SSD upgrade with all my Git Repos on the old drive.

A recursive Take Ownership fixed it all:

Two consecutive git status

UPDATE: Screenshots as requested by OP (Sorry was traveling and just got home)

Go to Security Tab of Directory or File Properties and click Advanced Go to Security of Directory or File and click Advance

Click on CHANGE button on the Owner row Click on CHANGE button on the Owner row

Enter your Username and click CHECK NAME. Note in Windows Username by default is your "Username" minus last character. eg. Johnny would be Johnn. Checkname will ensure your machine name is entered as well and to ensure you got your username correctly Enter your Username and click CHECK NAME

Check "Replace Owner on subcontainers and objects" for recursive ownership change Enable Recursive Change

5
  • 4
    Could you also screenshot how you did the "Take Ownership"? Those 2 git commants are consecutive and the operation in the middle is missing. Thnx! Commented Apr 29, 2023 at 21:14
  • 1
    Please read Why should I not upload images of code/data/errors? Instead, format code as a code block. The easiest way to do this is to paste the code as text directly into your question, then select it and click the code block button.
    – Chris
    Commented Apr 30, 2023 at 11:57
  • @XaviMontero Properties Dialog of any files/folder/Secruity/Advance Click on Change button on the OWNER line/type in your username minus last character/click CHECK NAME button to ensure Check the Replace owner on subcontainers and objects once OWNER is defined, Click OK or APPLY. Sorry am traveling so no computers with me
    – ShaguarWKL
    Commented May 8, 2023 at 14:17
  • @XaviMontero screenshots uploaded on main post. Hope this is what you were looking for.
    – ShaguarWKL
    Commented May 18, 2023 at 9:38
  • @ShaguarWKL That completely resolved my issue. I greatly appreciate your help!
    – Mohmd.h.bh
    Commented Aug 4 at 11:54
7

Unsure whether this applies to everyone experiencing this error using Windows.
I ran Git Bash as administrator and it no longer displayed the error for me when doing "git status".
I may have originally created the repository while running Git Bash as adminstrator. I can't remember unfortunately.

Best of luck

2
  • Upvoted this, kinda out of desperation :) , but didn't help in my case, unfortunately. (Of course I tried whitelisting the dirs first (initially one by one, then also with '*'), but that didn't work either...)
    – Sz.
    Commented Dec 17, 2022 at 21:27
  • 1
    This should be the accepted answer. It solves the problem after restart terminal as admin.
    – Oseack
    Commented Mar 21, 2023 at 1:49
7

the acutal issue is that when you run any command in any specific git repo, it checks the issuer id with that of the ownership of the repo files, if git finds any conflict then you may get an error like:

fatal: detected dubious ownership in repository at '/some/path'

the solution is either to change the ownership of the repository files to that of the bash user running the git command, or, if you understand that the ownership and group names are correct for the files in the specific repository then you can run:

git config --global --add safe.directory /some/path

which will securely resolve the issue. done.

5
  • This might not work on Windows, at least I can't imagine another reason for downvoting, but it looks like a correct and helpful solution for UNIX-like operating systems. Commented Oct 5, 2023 at 14:11
  • 1
    Just ran it on windows, FAIL... I'm running view History from Visual Stupid-o (Studio) and it is throwing this error, doesn't matter if I add that, or even add: git config --global safe.directory '*' --replace-all I still get the error.. Turning the Git_Test_Debug_Unsafe_Directories to true (??) seems like it wouldn't solve the issue either -but could cause it... Last time I solved it by changing the owner of 1GB of files.. Commented Nov 22, 2023 at 20:19
  • 1
    Changing the entire owner tree to me (instead of Admin), exiting VS and going back in and it is still giving the error... Feeling like doing 'find / -name 'git' -rm' except that this is windows... Ugh... getting tired of this thing biting me... it was working until I deleted the entire source tree (due to git checkout branch_name, followed by git status saying I was on a detached head, and there was no remote - huh? how did I check it out then huh? And if I just checked out a branch it isn't detached)... Commented Nov 22, 2023 at 20:29
  • it WORKS from the command line (running as Admin), like it always did before I wiped out the directory and re-cloned it. Commented Nov 22, 2023 at 20:33
  • 1
    you have to mark it as safe from inside visual studio using their UI, if you do it from the command line, it doesn't work... Nice Commented Nov 22, 2023 at 20:40
2

I have encounter this issue after copying the folder from another PC (both Windows).

To fix it open property of the folder, click tab "Security" and button "Advanced". In the dialog box appears, make yourself the owner.

2
  • 1
    Or copy paste the folder, and work on the pasted folder since you will be the owner of it. Commented Jan 26, 2023 at 10:47
  • 1
    @OlivierdeRivoyre Yes, this should also work, but only if you are copying from a file system with no access rights (e.g. FAT32). Commented Jan 27, 2023 at 13:46
2

A workaround for Windows command prompt:

takeown /r /d Y /f <repository name>

It's a workaround because I would rather have my repositories installed 'correctly'.

Additional Notes:

Sometimes, I don't experience this issue when running command prompt as Administrator.

1

I had similar issue when using WSL2 on Windows and code editor is PhpStorm. The Show Git logs are not working with the error "git reporting detected dubious ownership in repository"

Here is the fix:

  1. Open Windows Power shell
  2. type wsl + enter
  3. git config --global safe.directory '*'

enter image description here

Now the Git log is working. enter image description here

0

I also faced the same situation for github and came here. But none of them worked for me in Windows 11. My scenario is, after reinstall windows, "git pull" didn't work. Showed me the same error. My procedure:

Added New SSH Key: GO HERE

Then Added that SSH Key to Github: GO HERE

After using git pull again, I have to give that paraphrase (if any) and that pulling worked.

So, this will definitely work for those who have faced,

  1. New windows setup / reinstall windows
  2. Using Github and used SSH Key

Thank you.

0

Running wsl inside GitBash worked on my machine.

More discussion is here.

0

I just had this come up with using PHPStorm to access files that are on a WSL instance.

My issue was that I mounted the WSL instance to a drive letter and pointed the project to the drive instead of the true WSL file path U:\home\... vs \\wsl$\Ubuntu\home....

So to fix, just had to change the project location and all the git functions worked without have to silence the error.

-1

First of all, you check status type command: git status

then you saw this message enter image description here

now type these line of command that you have seen in the error line enter image description here

-5

simply add sudo in front of your command

sudo git status
1
  • If you are sure that you think this is a good solution, please elaborate why. Commented Oct 5, 2023 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.