You SSH into your server, run git pull like you've done a hundred times, and Git refuses to touch the repository at all:

fatal: detected dubious ownership in repository at '/home/shopuser/public_html'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/shopuser/public_html

Nothing's broken in your code, and the repo isn't corrupted. Git is just saying that the person running the command isn't the person who owns the folder. Here's why it happens, how to fix it properly, and when the "just add safe.directory" shortcut is fine (and when it isn't).

Symptom: what you'll see

Depending on your Git version you'll get one of two messages:

  • Newer Git: fatal: detected dubious ownership in repository at '/path'
  • Git 2.35.2 specifically: fatal: unsafe repository ('/path' is owned by someone else)

It usually shows up in situations like these:

  • A deploy script that used to work starts failing after an OS or package update pulled in a newer Git.
  • A cron job running as root runs git pull inside a user's home directory.
  • A PHP webhook (running as www-data, nginx or apache) calls git pull on a repo owned by your deploy user.
  • You restored a backup or rsynced files over as root and the ownership changed.
  • A Docker container running as root works on a bind-mounted folder owned by UID 1000 on the host.

Cause: why Git started doing this

In April 2022 Git patched a security hole (CVE-2022-24765). Before that fix, if you ran any Git command inside a directory, Git would happily read the .git/config it found there, or in a parent folder. That config can define hooks and settings that run commands. On a shared machine, another user could plant a .git folder somewhere you'd wander into, and your next git status would run their code as you. If "you" happened to be root, that's game over.

So Git now checks ownership before it trusts a repository. If the top-level folder of the repo (or its .git directory) is owned by a different user than the one running the command, Git stops. The safe.directory setting is how you tell it "I know, this one's fine."

Step 1: confirm who owns what

Before changing anything, compare the two users:

whoami
stat -c '%U:%G %n' /home/shopuser/public_html /home/shopuser/public_html/.git

Typical output when things are mismatched:

root
shopuser:shopuser /home/shopuser/public_html
shopuser:shopuser /home/shopuser/public_html/.git

Here root is running Git on shopuser's files. There's also the reverse case, where root-owned files ended up inside a user's folder after a restore. That looks like this:

shopuser
root:root /home/shopuser/public_html

Which case you're in decides the right fix.

Fix options, from best to "only if you must"

SituationBest fix
Files got the wrong owner (restore, rsync, copy as root)Fix ownership with chown
Root or a cron job runs Git on a user's repoRun Git as the owner with sudo -u
Web server user runs a deploy hookHand off to a script that runs as the owner, or a narrow safe.directory entry
Docker container with a bind mountRun the container with the host UID/GID
Shared folder you genuinely trust, one-offsafe.directory for that exact path

Fix A: correct the file ownership

If the repo should belong to shopuser and it doesn't, put it back. Run this as root:

chown -R shopuser:shopuser /home/shopuser/public_html

On cPanel servers, double-check the group. Some setups expect public_html itself to be shopuser:nobody with 750 permissions, so restore that afterwards if it's how your server is configured:

chown shopuser:nobody /home/shopuser/public_html
chmod 750 /home/shopuser/public_html

Git only looks at the owner, not the group, so the nobody group won't trigger the error again.

Fix B: run Git as the user who owns the repo

This is the cleanest fix when root is the one running Git. Don't teach root to trust the folder. Just don't use root:

sudo -u shopuser git -C /home/shopuser/public_html pull

For cron, put the job in the owner's crontab instead of root's:

crontab -u shopuser -e
*/15 * * * * cd /home/shopuser/public_html && git pull --ff-only >> /home/shopuser/git-pull.log 2>&1

You also avoid a second problem this way: when root runs git pull, every new file it creates is owned by root, and your site starts throwing permission errors later.

Fix C: add a safe.directory exception (the targeted way)

Sometimes a different user really does need to read the repo, such as a monitoring tool or a build user. In that case, add an exception for that one path, as the user who runs Git:

git config --global --add safe.directory /home/shopuser/public_html

A few things trip people up here:

  • --global writes to the current user's ~/.gitconfig. If you run it as yourself but the deploy runs as www-data, it does nothing for www-data.
  • Git ignores safe.directory in the repository's own .git/config. It only counts in system, global, or command-line config. That's deliberate: a malicious repo can't whitelist itself.
  • The path has to match exactly. /home/shopuser/public_html/ with a trailing slash, or a symlinked path like /var/www/site pointing at it, may not match. Use the path Git prints in the error.

For service users like www-data, whose home directory may not be writable, use the system-wide config instead (as root):

git config --system --add safe.directory /var/www/site

Or pass it only for a single command, without saving it anywhere:

git -c safe.directory=/var/www/site -C /var/www/site pull

To see what's already trusted:

git config --global --get-all safe.directory
git config --system --get-all safe.directory

Fix D: Docker bind mounts

If a container runs as root and works on host files owned by UID 1000, you'll hit this inside the container. Run the container as the host user instead:

docker run --rm --user "$(id -u):$(id -g)" -v "$PWD":/app -w /app alpine/git status

In Docker Compose, the equivalent is a user: "1000:1000" line on the service. If you can't change the user, add a safe.directory entry for the mount path in the image's system config.

What about safe.directory = '*'?

You'll see this all over forums:

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

It works because it switches the ownership check off completely for that user. On a single-user VPS where only you have shell access, the risk is low. But don't do it as root on a server with several cPanel accounts or shared users. That's exactly the setup the check was built to protect. A targeted path takes ten seconds longer and keeps the protection for everything else.

Webhook deploys: a safer pattern

A common setup is a deploy.php that runs shell_exec('git pull') as the web server user. Rather than making the web server own your code, give it permission to run one script as the deploy user and nothing else:

#!/bin/bash
# /usr/local/bin/deploy-site.sh  (owned by root, chmod 755)
set -e
cd /var/www/site
git pull --ff-only

Then a sudoers rule, added with visudo -f /etc/sudoers.d/deploy-site:

www-data ALL=(deploy) NOPASSWD: /usr/local/bin/deploy-site.sh

Your PHP hook calls sudo -u deploy /usr/local/bin/deploy-site.sh. Git runs as the owner, no exception is needed, and the web server can't do anything else as deploy.

Prevention

  • Pick one owner for each codebase (the cPanel user or a dedicated deploy user) and run every Git command as that user.
  • Avoid running git pull as root on site files. Use sudo -u instead.
  • When you restore or rsync as root, fix ownership straight after, or use rsync --chown=shopuser:shopuser.
  • Keep safe.directory entries narrow and specific, and check them now and then with --get-all.
  • Before upgrading the OS on a server with automated deploys, test the deploy once. A Git upgrade is the classic reason this error "suddenly" appears.

Frequently Asked Questions

Is the "dubious ownership" error a sign my server was hacked?

Almost never. It just means the user running Git doesn't own the repository folder. That's normally because of root, cron, a web server user or a restore. Check ownership with stat before assuming anything worse.

I added safe.directory but still get the error. Why?

Usually you added it for the wrong user. --global only affects the account that ran the command. Run whoami inside the failing script, or use --system. Also make sure the path matches the one in the error exactly, with no trailing slash and no symlinked path.

Can I put safe.directory in the repo's .git/config?

No. Git ignores it there on purpose, so a repository can't mark itself as trusted. Use system config, global config, or -c on the command line.

Will chown -R break my website?

Not if you set the owner your site already expects. On cPanel, that's the account's username (and public_html may need the nobody group). On a VPS, use whichever user PHP-FPM or your app runs as. Check one known-good file with stat first and match it.

Does this affect cPanel's Git Version Control feature?

Not normally, because cPanel runs those Git operations as the account user. You'll only see it if files inside the repo became root-owned, for example after a manual restore. Fixing ownership with chown clears it.