“Fatal: detected dubious ownership in repository” is a common error related to the unsafe repository in Git. In this article we will show you the cause of this error and the way to use a safe.directory to fix it.
How does the error “fatal: detected dubious ownership in repository” happen?

This error happens due to the many following reasons:
Firstly, it might be because you are trying cloning from another user. When accessing /var/www, such as when performing a Git command, a Git user often gets this error. This is because /var/www is owned by www-data, who is not a sudo user, and so access is restricted:
git branch
Secondly, you are using an Ubuntu user and an outdated version of Git. We will help you update git, but first and foremost, you should check the git version you are using:
git version
Finally, you are attempting to insert the disk into a Linux workstation and try to get a new repository on the new disk. For example:
git submodule update –init –recursive
How to solve the error?
Solution 1: Add a safe.directory
To solve “fatal: detected dubious ownership in repository” error, if because of the Git safe update, you will have to add into the safe directory the project directory with this command:
git config --add safe.directory 'project_path' --global
Or you can use this command to add all directories to the safe directory (this is not recommended):
git config --add safe.directory * --global
You shouldn’t disable this security measure unless you understand why it was implemented and why it would still be needed. Ideally, your repositories should be stored on a drive exclusively yours and not shared.
Disable this check if you only use your machine 100% of the time and store your repositories locally. This theoretically wouldn’t increase any risk. If the command above causes errors, you are not using an up-to-date Git version, so you have to update before using it. Check out our next solution to update Git.
Solution 2: Update Git version
Any “unsafe repository” errors will stop with the latest patch versions of Git v2.30-34. You can solve the problem by disabling safe directory checks in Git v2.35.3. To update git in Windows:
winget install --id Git.Git -e --source winget
To update git in Ubuntu or WSL, first, run:
sudo apt update
Now you can install the latest version of git by this command:
sudo apt install git
Solution 3: Change owner or Switch user
When attempting to clone from a different user, you should change this owner with the command:
chown -R <current_user> <repo_folder>
Or, use this command to switch to the correct user:
su here_currect_user_name
Summary
We have shown you how to deal with the “fatal: detected dubious ownership in repository” in Git. You can quickly solve this problem by checking Git ownership or Git settings carefully and finding out the reasons causing this problem in this tutorial.
Maybe you are interested:
- How to delete folder in Gitlab repository
- Git commit your changes or stash them before you can merge
- How to skip Git commit hooks

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R