gittermux

Git error detected dubious ownership needs add safe.directory everytime


I changed my user.name many times in git using command : git config --global user.name myname But i entered this command multiple times with different names and now it shows me error

fatal: detected dubious ownership in repository at '/storage/emulated/0/dev/git/C9-hp/test-/test'
To add an exception for this directory, call:
git config --global --add safe.directory 
        /storage/emulated/0/dev/git/C9-hp/test-/test

In some previous repos i just used git config --global --add safe.directory As it said. But i was trying to find the true answer and not just fix the problem for now using that command, so I asked it from ChatGPT. I don't know if it understood my problem or not, but here's what it gave me :

sudo chown -R yourusername:yourusergroup /storage/emulated/0/dev/git/c9-hp/test-/test
  498  chown -R yourusername:yourusergroup /storage/emulated/0/dev/git/c9-hp/test-/test
  499  chown -R C9hp:C9hp /storage/emulated/0/dev/git/c9-hp/test-/test

But my android phone should be rooted for that because I'm using Termux. Is there any way to fix this problem once for all my future repositories?

Edit : What does it contain ? I'm just learning git now and this is for test so there is 2 text file and one empty directory on it now. My local git repo is on internal storage, my android phone is Xiaomi Redmi 10 128GB with 4GB RAM.


Solution

  • You can use * to completely opt-out of the security check. It's going to be a bit useless on an Android system anyway.

    git config --global safe.directory '*'
    

    Why Android permissions != Unix permissions

    Even though it's based on Linux, Android isn't really a multi-user system like Linux, MacOS, or even Windows. There's one user—the person holding the phone.

    Evidently, Android marks most of the files writable by the user as -rwxrwxrwx root everyone. This might be an artifact of having been designed to run on SDcards with FAT-based filesystems, and/or this was a shortcut by the Android designers to avoid the headaches of having to modify permissions, with your thumbs no less. Either way, *NIX permissions don't apply.

    What is safe.directory? What does it do?

    Here's the relevant doc from git help config

    safe.directory

    These config entries specify Git-tracked directories that are considered safe even if they are owned by someone other than the current user. By default, Git will refuse to even parse a Git config of a repository owned by someone else, let alone run its hooks, and this config setting allows users to specify exceptions, e.g. for intentionally shared repositories (see the --shared option in git-init(1)).

    This is a multi-valued setting, i.e. you can add more than one directory via git config --add. To reset the list of safe directories (e.g. to override any such directories specified in the system config), add a safe.directory entry with an empty value.

    This config setting is only respected in protected configuration (see the section called "SCOPES"). This prevents the untrusted repository from tampering with this value.

    The value of this setting is interpolated, i.e. ~/<path> expands to a path relative to the home directory and %(prefix)/<path> expands to a path relative to Git's (runtime) prefix.

    To completely opt-out of this security check, set safe.directory to the string *. This will allow all repositories to be treated as if their directory was listed in the safe.directory list. If safe.directory=* is set in system config and you want to re-enable this protection, then initialize your list with an empty value before listing the repositories that you deem safe.

    As explained, Git only allows you to access repositories owned by yourself, i.e. the user who is running Git, by default. When Git is running as root in a non Windows platform that provides sudo, however, git checks the SUDO_UID environment variable that sudo creates and will allow access to the uid recorded as its value in addition to the id from root. This is to make it easy to perform a common sequence during installation "make && sudo make install". A git process running under sudo runs as root but the sudo command exports the environment variable to record which id the original user has. If that is not what you would prefer and want git to only trust repositories that are owned by root instead, then you can remove the SUDO_UID variable from root's environment before invoking git.