skip to Main Content

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 of what happened?

20

Answers


  1. 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.

    Login or Signup to reply.
  2. I got same issue and fixed by changing owner for directory.
    Please try
    chown -R <current_user> <repo_folder>

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. 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 "*"

    Login or Signup to reply.
  5. 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.

    Login or Signup to reply.
  6. Just run using sudo : sudo git status

    Login or Signup to reply.
  7. I had the problem with a library from the vendor folder. I just deleted the folder of the library and reinstall it.

    Login or Signup to reply.
  8. 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 chmod g+w -R .git
    
    Login or Signup to reply.
  9. 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.

    Login or Signup to reply.
  10. 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

    Login or Signup to reply.
  11. 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.

    Login or Signup to reply.
  12. 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()

    Login or Signup to reply.
  13. 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.

    Login or Signup to reply.
  14. 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.

    Login or Signup to reply.
  15. Just do your command from sudo rights like :
    sudo git clone XXX

    Login or Signup to reply.
  16. Encountered fatal: detected dubious ownership in repository at using Fork client on Windows against repository in WSL.
    Solution was to open git console inside Fork (pointing at 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 command inside WSL simply not worked as it changes .gitconfig inside the WSL.

    Login or Signup to reply.
  17. 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 form 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.

    Login or Signup to reply.
  18. 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.

    Login or Signup to reply.
  19. Happened to me after upgrading JetBrains Rider code editor. Before the upgrade Rider was launched by a shortcut as Administrator. Upgrade removed the shortcut’s config of "Run as Admin" which caused Rider’s Terminal to start giving me this git error. Running the application (Rider) as Admin again solved the git issue.

    Login or Signup to reply.
  20. Open git bash in your project Location and run below command for Windows OS for bypass security.

    git config –global safe.directory ‘*’

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search