31

I mounted a new hdd in my linux workstation. It looks working well. I want to download some repo in the new disk. So I execute git clone XXX, and it works well. But when I cd in the folder, and execute git submodule update --init --recursive. It failed with

fatal: detected dubious ownership in repository at '/media/data/users/jhu3szh/serialize'
To add an exception for this directory, call:

git config --global --add safe.directory /media/data/users/jhu3szh/serialize

I thought maybe it's just a slight warning, so I just executed git config --global --add safe.directory /media/data/users/jhu3szh/serialize. However, when I execute the git submodule again, more similar errors came out. There are many submodules in repo.

Can someone give me some explanation what happened? I searched the error information in google, but I can hardly get useful information. Thanks in advance.

8
  • Are you sure you have the proper permissions in the directory? Other posts regarding a similar problem suggest that it may be due to lacking permissions.
    – Nox
    Jul 14 at 9:50
  • 1
    It's not a question of the modes of files in the repository (though you should definitely not set them to 0777 in most cases, so you might want to undo your chmod if you can; unfortunately without restoring from backup, that's generally difficult). The complaint instead has to do with ownership, i.e., the user-ID who owns each of the various directories.
    – torek
    Jul 14 at 12:21
  • 2
    You can either ensure that all repositories are owned by the correct owner-ID (by not running anything with sudo if at all possible), or bypass the security (but if you do that, you're taking some level of risk as shown by the CVE's existence). To fix the ownership of existing repositories, use chown to change their ownership to the correct owner. Of course this requires the use of sudo—but at least you can use it just once, to fix this condition, and then stop using it...
    – torek
    Jul 14 at 12:22
  • 1
    To completely bypass the security, add * as a "safe" directory (note that this requires a Git version that supports *; 2..36 or later does, for instance).
    – torek
    Jul 14 at 12:25
  • 1
    @Koithé, not yet Jul 18 at 1:41

5 Answers 5

Reset to default
25

Silence all safe.directory warnings

tl;dr

Silence all warnings related to git's safe.directory system. Be sure to understand what you're doing.

git config --global --add safe.directory '*'

Long version

Adapted from this post on I cannot add the parent directory to safe.directory in Git.

I had the same issue and resolved it by disabling safe directory checks, which will end all the "unsafe repository" errors.

This can be done by running the following command1:

git config --global --add safe.directory '*'

Which will add the following setting to your global .gitconfig file:

[safe]
    directory = *

Before disabling, make sure you understand this security measure, and why it exists. You should not do this if your repositories are stored on a shared drive.

However, if you are the sole user of your machine 100% of the time, and your repositories are stored locally, then disabling this check should, theoretically, pose no increased risk.

Also note that you can't currently combine this with a file path, which would be relevant in my case. The command doesn't interpret the wildcard * as an operator per say– it just takes the "*" argument to mean "disable safe repository checks/ consider all repositories as safe".


1 - If this fails in your particular terminal program in Windows, try surrounding the wildcard with double quotes instead of single (Via this GitHub issue):
git config --global --add safe.directory "*"

16

I got same issue and fixed by changing owner for directory. Please try chown -R <current_user> <repo_folder>

4

Create a new directory on your disk where your current user is the owner of this new directory. In this new directory clone your git repo.

2

If same problem occurs on NTFS/Windows, make sure both parent of .git and .git folders are owned by exact user you run git from.

Just same group (Administrators) or only parent of .git may not work.

Upd: Permissions can be edited via right-click on the folder(s) → Properties → Security Tab → Advanced (bottom right of the window) → Owner.
Possibly disabling inheritance will be required, done in same window. This q/a have hints.

User you run git from can be checked in Resource Monitor: Task Manager → Performance → Open Resource Monitor (on bottom). May require enabling hidden "User Name" column.

2
1

Make sure you are using the correct terminal user. For me I had temporarily changed to the root user which would have caused issues. Changed back to standard user with su git-user and error went away.