Lorixen.
EN

Git 'detected dubious ownership'? Fix it without opening holes

Short answer: Git refuses to work in a repository owned by a different user than the one running the command, and stops with "fatal: detected dubious ownership in repository". It's a security check, not corruption: a repository's configuration and hooks can run commands, so Git won't trust one that someone else controls. The proper fix is to run Git as the user who owns the files, or change the files' owner to the user that deploys. If the mismatch is intended, add that one path to safe.directory in your global or system Git config rather than switching the check off everywhere.

Why Git checks ownership

A Git repository is more than files. Its .git folder holds configuration that can point to programs Git will run, and hooks that run automatically on actions like commit or checkout. If another user on the same machine controls that folder, they could make your Git run their code with your permissions.

Git's documentation for the safe.directory setting explains the default: Git will refuse to even parse the configuration of a repository owned by someone else, let alone run its hooks. Newer Git versions enforce this, so a server or container that worked for years can start failing after Git is updated.

Where it usually shows up

  • Deploy scripts run by a different user. The site files were cloned as root or your own login, but the deploy runs as www-data, deploy or the web panel's user.
  • Cron jobs. A scheduled git pull runs under a service account that doesn't own the folder.
  • CI and containers. The checkout step writes files as one user ID, then your build runs as another inside the container. A mounted folder from the host often has a different owner.
  • Shared folders. Several team members use one checkout on a shared server.
  • After copying or restoring. Restoring a backup or copying files as root changes ownership quietly.

Step 1: find out who owns what

Before changing anything, compare the repository owner with the user running Git. Use ls -ld on the repository folder and its .git directory, and whoami or id in the same context as the failing command. For a cron job or service, that context matters: run the check from the job itself, or look at the user it's configured to run as.

Once you know the two users, choose one of the fixes below.

Fix 1: run Git as the owner

If the files belong to the deploy user, make the deploy script run as deploy too. On a server, that can mean running the command through sudo -u deploy, setting the user in the systemd service or cron entry, or configuring the web panel's job to run as the site user. This keeps the security check working as intended and avoids touching Git settings at all.

Git's documentation also describes a special case: when Git runs as root through sudo, it checks the SUDO_UID value that sudo sets and also allows the original user's repositories. That's why sudo git ... sometimes works where a root cron job doesn't.

Fix 2: change the owner of the files

If the repository should belong to the deploying user, change its ownership recursively, including the .git folder. Be careful on web servers: your web server may need to read the files, and some folders such as upload directories may need to be writable by it. Plan the owner and group so the deploy user owns the code and the web server has only the access it needs.

For a checkout that several people genuinely share, Git's init documentation describes a --shared option for repositories intended for a group. A shared deployment checkout is usually better replaced by having one deploy user that everyone runs through.

Fix 3: mark the repository as safe

When the mismatch is intended and you trust the owner, add the path to safe.directory. Key points from Git's documentation:

  • It can be set more than once, so add one entry per repository with git config --global --add safe.directory /srv/app.
  • It's only read from protected configuration, meaning your global or system config or the command line, never from the repository's own config. That stops a repository from declaring itself safe.
  • Adding /* to a directory allows all repositories under it.
  • Setting it to * disables the check for every repository. Avoid this on shared servers.
  • An entry with an empty value resets the list, which is useful if a system-wide * was set and you want the protection back.

Remember that "global" means the global config of the user running Git. If your deploy runs as www-data, adding the setting to your own login's config won't help.

In CI pipelines

In containers, the cleanest fix is usually to make the job run as the same user ID that owns the checkout, or to set file ownership at the start of the job. If you add safe.directory instead, limit it to the workspace path the pipeline uses, not *. Because CI containers are thrown away after each run, the risk is lower than on a shared server, but a narrow entry costs nothing.

An illustrative example

A web agency in Halifax deploys client sites with a script that runs git pull in /var/www/client-site and then clears the cache. After a server update, deploys fail with the dubious ownership error. The folder was cloned as root, while the deploy script runs as the site user. The team changes the folder's owner to the site user, keeps the upload folder writable by the web server group, and moves the cron job to run as the site user. No safe.directory entry is needed, and every other repository on the server keeps the protection.

FAQ

Is my repository damaged?

No. The error is about trust, not data. Your commits and files are fine.

Why does it work when I run it by hand but fail in cron?

Your manual session and the cron job probably run as different users, or the job runs as root without sudo.

Is setting safe.directory to * dangerous?

On a single-user machine the risk is small. On shared servers it removes protection against other users' repositories, so list specific paths.

Does this happen on Windows?

Ownership checks exist there too, often with network drives or folders created by another account. The same fixes apply.

If your deploy scripts or pipelines broke after a Git update and you want them fixed without weakening security, tell us how your deploys run.

Sources

WhatsAppBook a call