Huihua Jiang
Huihua Jiang

Reputation: 4059

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

I mounted a new hard disk drive in my Linux workstation. It looks like it is working well. I want to download some repository 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 the repository.

What is the explanation for what happened?

Upvotes: 395

Views: 763366

Answers (30)

Sunil Sharma
Sunil Sharma

Reputation: 21

I just used this code and it solved all my errors.

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

This command is used to configure Git's safe directory settings globally.

Upvotes: 0

Lisandro
Lisandro

Reputation: 61

When the error "fatal: detected dubious ownership in repository" is returned from a command no directly executed by you but it is executed by some other program ie: PHP exec(). Adding the safe directory using --global won't work since the scope of --global is the current user.

In that case you need to assign the repo directory as safe in a system-wide scope by using the --system scope instead of --global.

ie: git config --system --add safe.directory /Path/To/Your/Repository

You will need sudo permissions to run this command since is going to modify the /etc/gitconfig file.

Upvotes: 3

Dinushe Jayasekera
Dinushe Jayasekera

Reputation: 51

I got the same issue and tried most of above and did not work. Then went through ".gitconfig" file which is normally located in C:\Users\your_username.

First it was like:

[safe]
directory = C:/Users/your_username/source/repos/somefolder
directory = C:/Usersyour_usernamesourcerepossomefolder2

As I noted missing "/" from path I changed it and saved adding it:

[safe]
    directory = C:/Users/djayasek/source/repos/telenet
    directory = C:/Users/djayasek/source/repos/MobiSIM_2024_03_01

Strange and it worked

Upvotes: 0

DVarga
DVarga

Reputation: 21809

Some additions to the answer from Stiin:

The command git config --global --add safe.directory '*' will add all repositories as safe.directory, but only for the current user.

If you would like to do the same for all users: git config --system --add safe.directory '*' will do the trick.

On Windows use "*" instead, as mentioned by many under this question.

This works of course without wildcard, by specifying the repo directory git config --system --add safe.directory /media/data/users/jhu3szh/serialize.

Upvotes: 1

Abhishek Parashar
Abhishek Parashar

Reputation: 91

Reinstall GIT and setup your email config again for GIT I didnt add the safe directory configs. Use email from same domain from which u have access to the submodule.

Upvotes: 0

Prakash Sahoo
Prakash Sahoo

Reputation: 1479

I got the same issue and fixed it by changing the owner for the directory, e.g.,

chown -R <current_user> <repo_folder>

Upvotes: 147

Ahmed Nabil
Ahmed Nabil

Reputation: 19036

If you have sudo access, you can use the ownership:
sudo -u OWNER git COMMAND

Examples:
sudo -u jenkins git status
sudo -u adam git clone ...
...

Upvotes: 0

halt9k
halt9k

Reputation: 953

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

Just the same group (Administrators) or changing only the parent may not work.

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 the same window. This Q&A has hints.

The user you run git.exe from can be checked in Resource Monitor: Task Manager → Performance → Open Resource Monitor (on bottom). May require enabling hidden "User Name" column.
If Git related executables are missing there, make a dummy Fetch or Pull while Resource Monitor is opened to ensure they were added.

Upvotes: 52

This is how I solved mine:

Run the following in your Android Studio terminal:

git config --list --global

After seeing duplicate directories listed, I decided to edit the global file by running:

git config --global --edit

My issue was with duplicate configurations in the .gitconfig global file listed. So I simply edited by deleting all of them till [safe] directory = * was left.

This configuration marks all directories as safe, provided you didn't install Git on a USB drive, but your local hard drive.

If you had set Notepad++ as your default Git editor during installation, then simply save the Notepad++ file you just edited. Else, if the editing stayed in the console, simply type :wq to save and close the file after editing.

The last step is to restart your IDE(Android Studio in this case). Use this video as a guide too Git config tutorial. Where is the Git config file?

Upvotes: 0

HelloGoodbye
HelloGoodbye

Reputation: 3922

On Windows 10, I solved the same problem simply by creating a copying of repository and using that instead.

In Git Bash (from a working directory outside of the repository):

cp -r <path-to-repository> <temporary-path>
rm -rf <path-to-repository>
mv <temporary-path> <path-to-repository>

Upvotes: 0

pfraviga
pfraviga

Reputation: 11

If you are using Sublime Merge on Windows and you just want to silence this warning, you need to edit the gitconfig file used by Sublime Merge, which is usually located in the installation directory in the Git\etc subdirectory (something like C:\Program Files\Sublime Merge\Git\etc\gitconfig). There you can add the [safe] section (if not already present) and either the suggested path, or the wildcard '*', as suggested by other answers.

See an example below:

[safe]
    directory = *

or

[safe]
    directory = 'C:/Users/Username/Projects/Whatever'

Of course you can add multiple directories.

Upvotes: 1

Markus Hoffrogge
Markus Hoffrogge

Reputation: 91

If you need this working for service users not having a home directory - like, e.g., www-data, you should consider to Git configuration it via Git system configuration:

sudo git config --system --add safe.directory /my/repos/repofolder

Upvotes: 6

cmucheru
cmucheru

Reputation: 159

If you clone a repository with a given user and then try to pull changes with a different user, the error arises. Change the user to the user you cloned the repository with, like this:

su <user>

Then proceed, for instance,

git pull

Upvotes: 1

WhiteKnight
WhiteKnight

Reputation: 5066

It seems that two folders are being checked by Git: the .git folder and the folder containing it. Both must be owned by the current user. The other folders and files of the folder containing the .git folder can be owned by other users if there is a need. Therefore

  1. Check the current user, e.g., run whoami
  2. Check the owner of the .git folder, e.g. run ls -al .git
  3. Evaluate the impact of changing the ownership of the .git folder if it is not the current user. If you want to also change the group, you might want to check what groups a user is in, e.g., running id -Gn username or groups username
  4. Update the owner of the Git repository if it is safe by running sudo chown current_user:appropriate_group .git with the appropriate current_user and appropriate_group, e.g. run sudo chown ubuntu:ubuntu .git
  5. Check the owner of the folder containing the .git folder, e.g., run ls -al ./
  6. Evaluate the impact of changing the ownership of that folder if it is not the current user. If you want to also change the group, you might want to check what groups a user is in, e.g., running id -Gn username or groups username
  7. Update the ownership of that folder by running sudo chown current_user:appropriate_group ./ with the appropriate current_user and appropriate_group, e.g., run sudo chown ubuntu:ubuntu ./

Ensure that if you do change the owner of the folder containing the .git folder that permissions are still setup to work as you prefer, i.e. whether the previous owner retains the same read, write and execute permissions with the current group and everyone permissions. If they have lost permissions, either consider adjusting the group or other or revert the folder ownership change and use the git config --global --add safe.directory path/to/repository solution instead.

Upvotes: 4

Alex Peter
Alex Peter

Reputation: 1

For me, the solution was to run the console (or Sourcetree in this case) as administrator.

Upvotes: -1

Kaustubh Navale
Kaustubh Navale

Reputation: 47

Open Git Bash in your project location and run the below command for Windows OS for bypassing security.

git config --global safe.directory '*'

Upvotes: 2

izy
izy

Reputation: 1253

It happened to me after upgrading JetBrains' Rider code editor. Before the upgrade, Rider was launched by a shortcut as Administrator. The upgrade removed the shortcut's configuration of "Run as Administrator" which caused Rider's Terminal to start giving me this Git error. Running the application (Rider) as Administrator again solved the Git issue.

Upvotes: 1

stats con chris
stats con chris

Reputation: 347

If you are lost, Git clone the file with different usernames. For example, try with sudo git clone. This gives ownership to root.

If you choose instead just git clone... then the ownership goes to the current username... One of them should work, likely you are mixing them up, and you need to type sudo git branch -a to avoid this error.

Upvotes: 0

Tms91
Tms91

Reputation: 4214

This error is raised when you make an attempt to Git initialize (and then run any other Git command) a folder that is not child of /home/my_user/.

In your case, the folder is serialize, which is under /media/... .

You can stop the alert from being raised by just moving the folder under /home/my_user/ .

Otherwise, you can also change the owner of my_project_folder , as @Prakash Sahoo suggests.

Upvotes: 0

klapshin
klapshin

Reputation: 811

I encountered fatal: detected dubious ownership in repository at using the Fork client on Windows against a repository in WSL. The solution was to open a Git console inside Fork (pointing at the Git Windows client used by Fork) and execute:

git config --global --add safe.directory '%(prefix)///wsl$/Ubuntu-22.04/home/username/code/my-repo-name'

Running the command inside WSL simply did not work as it changed .gitconfig inside WSL.

Upvotes: 0

Arvind Chourasiya
Arvind Chourasiya

Reputation: 17432

I ran the below command in Git Bash to solve it:

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

Close and reopen Visual Studio Code if required.

Upvotes: 18

Serious Angel
Serious Angel

Reputation: 1572

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()

Upvotes: 2

Kleyton Roan
Kleyton Roan

Reputation: 621

If the OS is Windows, you have to take the ownership of the file with the command

takeown /F <dir/*> /R

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

/R parameter means recursive. It applies the owner to the current logged user to all files and subfolders too

<dir/*> is the directory to take ownership of.

Upvotes: 62

PalFS
PalFS

Reputation: 831

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 directory /var/repo which was my current user. The Phabricator web server executes the commands using user "www-data". I then changed the ownership of the "/var/repo" directory to the following:

sudo chown www-data:ubuntu -R repo/

After that it worked fine.

Upvotes: 1

VladSavitsky
VladSavitsky

Reputation: 563

I've got the same error message on Ubuntu/LEMP when executing Git's commands from PHP. Nothing suggested in previous answers helped to fix problem.

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

In my case, Git commands were executed by the 'www-data' user (which is a web server user):

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

To restore the ability to work with Git from the command line, you need to allow the group to write:

sudo chmod g+w -R .git

Upvotes: 7

Alin Pop
Alin Pop

Reputation: 481

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

Upvotes: 0

Stiin
Stiin

Reputation: 5101

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

Upvotes: 488

harri
harri

Reputation: 574

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

Upvotes: 12

Centillion
Centillion

Reputation: 381

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.

Upvotes: 8

Yashas
Yashas

Reputation: 41

None of the answers here worked for me; I ran:

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

and

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

and I was still getting the error in IntelliJ, because I was doing git pull using the admin User. The only solution that worked for me is to directly go to user page Path:

C:\Users\Your Username\\.gitconfig (add your username here)

and edit the gitconfig file. I added:

 [safe]
    directory = *

and it worked for me.

Upvotes: 4

Related Questions