Recently I found an error log while using apache 2, which is abruptly stopped my program, unsafe repository (‘/home/repon’ is owned by someone else). Here is a snippet of the error I got:
unsafe repository ('/home/repon' is owned by someone else)
Why unsafe repository (‘/home/repon’ is owned by someone else) error is seen?
The error , ‘unsafe repository (‘/home/repon’ is owned by someone else) is seen because you are having a configuration issue due to recently introduced constraints to not easily get trapped in a security issue. You can find and read more about the new Git safety changes here.
The error may be caused if you had git rev-parse --symbolic-full-name --abbrev-ref HEAD'
in your PHP code, because the new Git safety change does not allow www-data
to run this Git command.
A simple solution to fix this warning is to trust the Git directory (do it if you know the directory contents are safe)
A detailed solution to fix the unsafe repository (‘/home/repon’ is owned by someone else) error is provided below:
How to fix the error unsafe repository (‘/home/repon’ is owned by someone else)?
To fix unsafe repository (‘/home/repon’ is owned by someone else) error, trust the Git directory in your project file or make changes to the user access or change the Git repository owner to www-data
by running the code sudo chown -R www-data:www-data /home/repon. This would fix the error, unsafe repository (‘/home/repon’ is owned by someone else).
If still error unsafe repository (‘/home/repon’ is owned by someone else) persists, downgrade the Git Version using the apt install git-man=1:2.17.0-1ubuntu1 git=1:2.17.0-1ubuntu1 command as an alternate and temporary solution.
The error, unsafe repository (‘/home/repon’ is owned by someone else) started showing up with the release of the latest Git security update (Git 2.35.2).
There are four below mentioned solutions available to fix the unsafe repository (‘/home/repon’ is owned by someone else) error:
Method 1: Trust the Git Directory
Trust the Git directory in your project file. Maintain caution while doing so as some directory contents may be unsafe and trust the directory only if you are sure about the contents. You can trust a repository by writing the following commands.
git config --global --add safe.directory /home/repon
This is added to the safe
group to file ~/.gitconfig as displayed in the following example:
[safe]
directory = /home/repon
Method 2: Make changes to User access
To make changes to the user access please run the following command as shown below
sudo -u ubuntu -- git status
please be cautious as this requires the current user www-data
to have permission to execute the given Git command as user XYZ
(assuming XYZ is the repository owner). If you wish to follow this method, you will need to add a new file inside /etc/sudoers.d/
with the contents given below:
www-data ALL=(ubuntu) NOPASSWD: /usr/bin/git
This may induce some security implications, so please refer to your security person first.
Method 3: Change Git Repository Owner
You can change the Git repository owner to www-data
by running the code snippet given below
sudo chown -R www-data:www-data /home/repon
Method 4: Downgrade Git Version
If none of the other options are working try downgrading your 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: On Windows, it seems that all Git repo on ejectable drives are unsafe, and even changing their ownership does not seem to work.
Conclusion
To fix the error unsafe repository (‘/home/repon’ is owned by someone else); try to trust the Git directory(do it if you know the directory contents are safe) or try to run the command as the correct user or you can also try to change the Git repository owner to www-data
. If none of the above mentioned methods do not work downgrade Git as a temporary solution.