Git 子模块更新失败,“致命: 检测到存储库中的可疑所有权位于”

我在我的 linux 工作站上安装了一个新的 hdd。看起来效果不错。我想在新磁盘上下载一些回购文件。所以我执行 git clone XXX,它工作得很好。但是,当我在文件夹中 cd,并执行 git submodule update --init --recursive。它失败了

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

我想也许这只是一个小小的警告,所以我刚刚执行了 git config --global --add safe.directory /media/data/users/jhu3szh/serialize。但是,当我再次执行 git 子模块时,会出现更多类似的错误。回购中有许多子模块。

有人能给我解释一下发生了什么吗?我在谷歌上搜索了错误信息,但几乎得不到有用的信息。先谢谢你。

120827 次浏览

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.

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

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.

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

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.

Just run using sudo : sudo git status

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

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

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.

If the SO is Windows, you have to take the owner of the file with the command

takeown /F <dir/*> /R

/F parameter is the file, with the * wildchar will apply to all files and folder;

/R parameter means recursive, it's apply the owner to the current logged user to all files and subfolders too

If the command you are running already failed once or there was a previous version of the package in the install folder, try removing the corresponding package's folder before trying again or look for further solutions. Here, jhu3szh/serialize for example if it corresponds to your package name.

It should be possible to restore the ownership. For example:

sudo chown -v "$( id -u; ):$( id -g; )" .;
sudo chown -v "$( id -u; ):$( id -g; )" -R .git;
find '.git' -type d -exec chmod -v 775 {} \;;
find '.git/objects' -type f -exec chmod -v 444 {} \;;
find '.git/hooks' -mindepth 1 -maxdepth 1 -type f -exec chmod -v 775 {} \;;
find '.git' -type f ! -path '.git/objects/*' ! -path '.git/hooks/*' -exec chmod -v 664 {} \;;

Related: ensure_valid_ownership()

My problem was that I was not running as sudo. I usually set at the beginning sudo bash and do not think about sudo command. So it made me a lot of headache before I realized that I skipped my common step.

I ran below command on git bash to solve it

git config --global --add safe.directory C:/User/username/source/myproject

Close and reopen VS Code if required.

Just do your command from sudo rights like : sudo git clone XXX