98

I found error log from apache2 that is fatal: unsafe repository ('/home/repon' is owned by someone else)

It happens because I have git rev-parse --symbolic-full-name --abbrev-ref HEAD' in PHP code and it looks like the new git safety change no longer allows www-data to run this git command.

git config --global --add safe.directory /homerepon does not work. Does anyone have a workaround to solve this issue?

Git version: 2.35.3

PHP version: 7.4

Apache2 version: 2.4.41

pppery
  • 3,550
  • 19
  • 28
  • 41
XiaoFangyu
  • 991
  • 1
  • 2
  • 4
  • 3
    This is closely related to [Q1](https://stackoverflow.com/q/71855882/1256452) and [Q2](https://stackoverflow.com/q/71849415/1256452); see both questions and all the answers there. – torek Apr 17 '22 at 12:34
  • Also for an educated decision, read the URL you referenced in your question and study the git documentation for the parts it references. You're just having a configuration issue due to recently introduced constraints to not easily get trapped in a security issue. – hakre Apr 17 '22 at 12:40
  • Related (more Windows oriented): *[I cannot add the parent directory to safe.directory in Git](https://stackoverflow.com/questions/71849415/)* – Peter Mortensen Jun 04 '22 at 14:05

16 Answers16

70

This started appearing with the release of the git 2.35.2 security update which fixes vulnerabilities described here. credits @Juan-Kabbali

Here are 4 possible solutions:

  • trust git directory (do it if you know the directory contents are safe)
git config --global --add safe.directory /home/repon

This adds to ~/.gitconfig the safe group as shown in this example

[safe]
    directory = /home/repon
  • run the command as the correct user, for example:
sudo -u ubuntu -- git status

Note: This requires user www-data to have permission to execute the git command as user ubuntu (assuming ubuntu is the repository owner). For this to work, you will need to add a new file inside /etc/sudoers.d/ with the following contents:

www-data ALL=(ubuntu) NOPASSWD: /usr/bin/git

This may have security implications so refer to your security person first.

  • change git repository owner to www-data
sudo chown -R www-data:www-data /home/repon
  • downgrade git as a temporary solution. For example in Ubuntu:
apt install git-man=1:2.17.0-1ubuntu1 git=1:2.17.0-1ubuntu1

Note: At least on Windows, it appears that all git repositories on ejectable drives are considered unsafe and changing ownership does not seem to work.

smoore4
  • 3,838
  • 3
  • 31
  • 49
8ctopus
  • 1,229
  • 1
  • 9
  • 19
55

For Windows I had to do the following:

  1. Right click on the git repository folder on which the error occurs
  2. Select the security tab, then choose "Advanced" (left picture: press "Erweitert")
  3. Check the owner properties (right picture: "Besitzer") in the top area of the new opened window and adapt it (right picture: press "Ändern"), this must be your working Windows account
  4. Press OK and wait until rights have been set, then the Git error message should be history

enter image description here

This solution works also if you move or rename the directory afterwards. In my opinion you should prefer this solution over

git config --global --add safe.directory <repo-path>

which you have to do each time where you perform changes on the directory name. You can also adapt manually the .gitconfig file in

C:\Users\<username>\.gitconfig

once you added to the safe list.

Huber Thomas
  • 561
  • 1
  • 4
  • This solution worked for me, because the repository folder was owned by a different user. – natenho Apr 23 '22 at 06:36
  • That fixed some of my local repos. They were owned by a user group. I did not need to change any git config. – tobster Apr 25 '22 at 08:02
  • 5
    This should be the defacto solution on Windows. – LunicLynx Apr 25 '22 at 10:15
  • 1
    Thanks for Windows solution. The number 2 it is **Advanced** in English not **extended**. – vee May 18 '22 at 11:40
  • This should be the answer – Squibly May 19 '22 at 01:17
  • While this works, this is less than ideal. For me, these repos were owned by Administrators, not m specific account (but which is an Administrator). Furthermore, I am the only real user on this PC. I have close to a hundred repos, changing each manually will be very tedious. – Jahmic May 26 '22 at 07:08
  • 1
    In my case folders that had been restored in 2021 after reformatting and reinstalling Windows did not belong to my user account anymore. Apparently checking ownership is a new feature of GIT stating with version 2.35.2.windows.1. Projects created since the restore continued to work normally. Reclaiming ownership of the top level of the project folders and including "subcontainers and objects" took care of it.. It took a minute or two to process, but it was just one time. – Rich Shealer May 26 '22 at 14:35
  • "C:\Users\" is dependent on the Windows version and in which context Windows is used. Perhaps generalise your answer to use the appropriate environment variable, etc.? – Peter Mortensen Jun 05 '22 at 09:29
  • A lead may or may not [be `%USERPROFILE%`](https://stackoverflow.com/questions/58754860/). – Peter Mortensen Jun 05 '22 at 10:10
51

This is because of the git safe update.

To make Git trust any directory you can run this in powershell:

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

In bash you should escape the * to avoid expansion:

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

Support for * was only added in Git 2.36 as mentioned at: https://github.blog/2022-04-18-highlights-from-git-2-36/ and by genonymous in the comments.

If you just trust one directory, you can run this command

git config --global --add safe.directory your-directory
night
  • 527
  • 1
  • 2
4

I had a similar issue - a web application that uses git could not access the repository.
Running the suggested command (git config --global --add safe.directory /repo/path) didn't work as well, because I ran it as 'me', not as 'www-data' user.
The solution was in fact really simple - I created .gitconfig file in /var/www directory (which is home for www-data user in my case) and put

[safe]
        directory = /repo/path

there.

  • 1
    This should be the definitive answer for people running nginx on ubuntu. Note you should also ensure the ownership of the /var/www/.gitconfig is www-data by doing sudo chown www-data .gitconfig – Timothy C. Lethbridge May 06 '22 at 15:20
4

None of the above solutions worked for me but changing the ownership of the repository did. I'm running Ubuntu 20.04.4 LTS and I ran the following command:

sudo chown -R username:group directory
Ayush
  • 376
  • 4
  • 13
2

I may be stating the obvious, but I think it's worth mentioning that running git config --global --add safe.directory /home/repon needs to done for the www-data user.

Problem 1: www-data's HOME directory is /var/www, so having a .gitconfig file there may be a security risk (divulging server paths / config).

Problem 2: with Apache/Ubuntu 20.04, the HOME environment variable is not defined by default (/etc/apache2/envvars unsets it) so the config is not getting picked-up (git config --global fails with fatal: $HOME not set).

I managed to fix the problem by adding the repository to git's system config, i.e. git config --system --add safe.directory /home/repon.

dregad
  • 983
  • 7
  • 18
2

As a part of automation, Our scenario involved invoking one script multiple times and we don't know the workspace upfront.

So, in our case, git config --global --add safe.directory * created multiple entries in ~/.gitconfig.

git config --global --replace-all safe.directory '*' helped us ensuring no duplicate entries.

urug99
  • 21
  • 2
0

I had a similar problem, with Phabricator not being able to display the content of my repositories (git log failed because of the same reason as yours). I could not figure out which user was running the git command, so I could not come up with a proper solution, until I realized I could edit/create a global git config file for all users.

I created the file with: sudo vi /etc/gitconfig.

and put this inside:

[safe]
        directory = /home/opt/phabricator_repo/1
        directory = /home/opt/phabricator_repo/4
        directory = /home/opt/phabricator_repo/5

OS: Ubuntu 20.04

PJ127
  • 716
  • 8
  • 20
0

This happens if you have a different user who owned the directory. For example, your git repo is located in /var/www which is owned by www-data. Now, when you are signed-in/using a non-sudo user account and you go to /var/www to perform git actions such as

$ git branch

you will get this error so make sure you have appropriate directory permission. You can change the directory ownership by running CHOWN or add your current user to the group to which the directory owner belongs to

Rex Bengil
  • 40
  • 3
0

If you are on linux and prefer explicit allow-listing, you may achieve it manually by editing the Git config, (e.g. using nano or vim). Just put the folders allow-list into the [safe] section of the config file:

$ nano ~/.gitconfig

And here is a python script to prepare the allow-list:

from glob import glob

def println(my_list):
    print("\n".join(map(str, my_list)))

git_folders_list = sorted(glob("~/git/*", recursive=True))

println(["directory = " + d for d in git_folders_list])
mirekphd
  • 2,771
  • 21
  • 35
0

I had this problem on windows with Sublime Merge, I was trying to apply some solutions mentioned here, they didn't work so I said: if the problem is with the folder I must create a new one, so copy and paste the project folder, delete the old one, rename the copy by the old name and that was it!. I guess this should work on linux too and when making the copy of the project folder it is created with the correct owner.

Leoalv
  • 25
  • 8
0

Changing the owner of the top level directory fixed it.

Running Laravel on a local Ubuntu LAMP stack, my setup includes the command

sudo chown -R www-data /var/www/dirname

But with www-data owning the dirname, git gave the above error. To fix it, I only had to change the owner of the top level dirname, and the .git directory:

sudo chown myUserName /var/www/dirname

sudo chown -R myUserName /var/www/dirname/.git

Debbie V
  • 169
  • 2
  • 4
0

In addition to the accepted answer, for those who are getting "error: wrong number of arguments, should be 2" under Windows, use double quotation marks instead of single quotes when providing the directory argument.

e.g.:

git config --global --add safe.directory "D:/Source/Repos/SampleProject"
KJH
  • 2,032
  • 15
  • 20
Pasha
  • 21
  • 4
0

sudo chown -R [username]:[group directory]

that really works for me (macbook air m1)

mak
  • 409
  • 1
  • 4
  • 21
0

This was really frustrating.

Adding to the gitconfig worked, but who wants to do that seemingly every directory?! Craziness!

For me, the answer:

---> Don't use sudo!

For many cases, using sudo doesn't hurt, and it can be habitual if you are moving between things your user can touch, and things needing more access.

If my user created a repo and then I used "sudo git...", I got the error. I also had the "sudo git..." part in an alias, so it wasn't obvious that I was even using it.

Phreditor
  • 429
  • 4
  • 4
0

Alongside @Huber Thomas answer for Windows I had to use Powershell/CMD since I had a bunch of files in an scm folder I'd moved from one location to another. The TAKEOWN command handled this well (if a little slowly).

  1. Open CMD as yourself (don't need to be an administrator)
  2. Navigate to the repo folder (or parent if you have multiple)
  3. Run takeown /f . /r /d Y

This will recursively work through all folders at your current folder and set the ownership to the logged in user (presumably you).

tlbignerd
  • 955
  • 8
  • 19