31

According to this QA, we may use safe.directory argument to add directory to be marked as whitelist, due to latest CVE found on git. But it seems there is no way to add certain dirs recursively.

I have so many repositories to add, so i want to use recursive add instead, if the feature is exist. The repositories mostly placed on my mounted NTFS disk on ubuntu, so the owner of files inside is always root. Looks like the latest update restricts git operations if the logged in user is not match with owner of the git directory by showing error such fatal: unsafe repository ('/media/data1/project1/si/project' is owned by someone else.

hakre
  • 193,403
  • 52
  • 435
  • 836
adib-enc
  • 433
  • 1
  • 5
  • 6

2 Answers2

50

From Git 2.36, you can also add * representing 'all' to the safe.directory. It's not recursive as you asked, but it may help depending upon your situation i.e.

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

See https://github.blog/2022-04-18-highlights-from-git-2-36/ and search for safe.directory.

EDIT: As suggested by zcoop98, we should add double quotes around '*'.

Community
  • 1
  • 1
Swissmant
  • 519
  • 4
  • 8
  • 3
    I am getting this error ```wrong number of arguments, should be 2``` – Kunal Tyagi Apr 23 '22 at 17:32
  • You have the asterisk(*) at the end of the statement, right? That counts as the 2nd argument. – Swissmant Apr 25 '22 at 13:46
  • 14
    Depending on your CLI, you may need to put the `*` in single `'*'` or double `"*"` quotes. – zcoop98 Apr 27 '22 at 21:30
  • I couldn't get it to work in cygwin with *, '*' or "*". It worked in plain windows cmd prompt as * – Bill Tarbell Dec 30 '22 at 19:18
  • That (and nothing else) did it for me on Windows 11, thanks! – charlie Mar 24 '23 at 08:41
  • You should have the asterisk in the end of the command, if you miss it, then it complains about the number of arguments. – ersegun May 31 '23 at 06:39
  • This worked for me to allow WSL2 (windows subsystem for linux) share to work whether I am in a WSL linux console or in windows – Don Jul 07 '23 at 17:34
  • the --global config for windows is in the user directory. However this message happens mostly when you use 2 or more different accounts to access the git. So in that case you need to add it to both accounts. Another option is to add it to the gitconfig in C:\Program Files\Git\etc – jk Registraties Aug 18 '23 at 14:52
9

What I did for now, but may not be the perfect solution, is to find all .git folders and add them through a find command.

find /full/path -name '.git' -type d -exec bash -c 'git config --global --add safe.directory ${0%/.git}' {} \;

Want to remind, that it is necessary to add the full path in the find command, so it will resolve the full path.

ersatzhero
  • 106
  • 3
  • 2
    If you have too many sub directories (like I do) it's worth adding the `-maxdepth` option to speed up execution. – dcg Apr 14 '22 at 15:36
  • submodules' .git is a text file, so `-type d` might not be required. – ciel Mar 03 '23 at 15:35