Fatal: unsafe repository ( '...' is owned by someone else).

git sysadmin vscode

A Git security vulnerability was announced https://github.blog/2022-04-12-git-security-vulnerability-announced/

As a result, if the folder your git repository is in is owned by root and you are calling a git action from apache as www-data you will get the error:

fatal: unsafe repository ('/this/directory' is owned by someone else)
To add an exception for this directory, call:

        git config --global --add safe.directory /this/directory

This error will also prevent you using git functions in Visual Studio Code. You will see:

The folder currently open doesn't have a git repository.  You can initialize a repository which will enable source control features powered by git.
[Initialize Repository]
...

More on the VSCode issue can be found here https://github.com/microsoft/vscode/issues/148023#issuecomment-1110752612

So, like the instructions say, if you add the safe directory, you are able to use git as another user - BUT

I did find that if the directory is owned by root, you will still see the error if you try calling git as www-data

To fix the issue:

chmod www-data:www-data /this/directory

Where /this/directory is the directory containing your repository.

Moral of the story is stop cloning or creating git repos as root!