git submodule update failed with 'fatal: detected dubious ownership in repository at'

Viewed 49729

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.

9 Answers

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 "*"

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

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.

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.

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.

I've got the same error message on Ubuntu/LEMP when executed git's commands from PHP. Nothing suggested above helped to fix problem.

Solution: You need to set correct owner of the hidden '.git' folder.

In my case git comands was executed by 'www-data' user (which is a web-server user):

sudo chown www-data:<current_user> -R .git

To restore ability to work with git from command line you need to allow group to write:

sudo chown g+w -R .git

I had the problem with a library from the vendor folder. I just deleted the folder of the library and reinstall it.

For anyone having this problem in phabricator here is the solution that worked for me.

Background: Phabricator was working fine for me until this happened. When I open any repository to get the clone URL it throws me this error:

fatal: detected dubious ownership in repository at '/var/repo/3'

Solution: I checked the permissions and ownership of dir /var/repo which was my current user. Phabricator web-server executes the commands using user "www-data". I then changed the ownership of the "/var/repo" dir to the following:

sudo chown www-data:ubuntu -R repo/

After that it worked fine.

Just run using sudo : sudo git status

Related