Blog

Fixing git's “detected dubious ownership in repository”: check the owner before adding safe.directory

When git pull or git status stops with fatal: detected dubious ownership in repository, the cause is a mismatch between the repository's owner and the user running Git. I reproduced it with Git 2.43 to check that adding safe.directory still ends in Permission denied if you cannot write, that sudo git pull leaves root-owned files behind, and how a trailing slash or /* is handled. Then I explain when to fix the owner, when to run Git as the owner, and when to register an exception.

A folder marked with a yellow owner badge; a dotted line from a blue badge on the right stops in front of an orange shield, while an arrow from a green band at the lower right curves around into the folder

I wanted to update an app at /srv/app on a web server, so I logged in as the deployment user and typed git pull. This is what I got, and nothing else would work.

$ cd /srv/app
$ git pull
fatal: detected dubious ownership in repository at '/srv/app'
To add an exception for this directory, call:

	git config --global --add safe.directory /srv/app

“Dubious ownership” means the owner looks suspicious. The owner of the repository directory is not the user currently running Git, so Git refuses to read that repository. git status and git log fail the same way, with exit code 128.

The message spells out a fix, git config --global --add safe.directory /srv/app, and that is also the first thing a search turns up. It does make the error go away. In this example, though, the next git pull stops with a different error, because the right fix depends on why the owner is different.

In this article I explain why the error appears and how to check the owner, and then try three approaches, split by the situation each one fits: change the owner to yourself, run Git as the owning user, or make an exception with safe.directory. The output comes from Git 2.43.0 on Ubuntu 24.04. /srv/app is owned by www-data (the user the web server runs as), and I am working as the deploy user.

Why Git refuses other people’s repositories

This check arrived in Git 2.35.2, released in April 2022, in response to a vulnerability, CVE-2022-24765.

Git looks for .git starting from the current directory and moving up through the parents, then reads the configuration file of the repository it finds (.git/config). Some settings, such as core.fsmonitor, can make Git run an external command. On a machine shared by several people, someone could place a .git in a shared location and get arbitrary commands run by anyone else who used Git below it. GitHub’s announcement gave the example of someone creating C:\.git\config on Windows.

So Git stopped reading the configuration of repositories owned by another user. Without its configuration Git cannot work with the repository, so it stops at the very first git status. Having stopped, it lets you mark only the repositories you share on purpose as exceptions with safe.directory. That is how Git is designed.

In other words, the error does not mean anything is broken. It is Git telling you it has stopped on the side of safety.

First check the owner and who you are

Before choosing a fix, check who owns the repository and which user you are running Git as.

$ ls -ld /srv/app /srv/app/.git
drwxr-xr-x 3 www-data www-data 4096 Nov 26 23:52 /srv/app
drwxr-xr-x 8 www-data www-data 4096 Nov 26 23:52 /srv/app/.git
$ id -un
deploy

The third column is the owner. Here www-data owns it and deploy is working on it, so they do not match. Git compares the owners of the top directory of the working tree and of the .git directory. When I tried it, the same error appeared if just one of them belonged to someone else. Git does not look at the owners of the files inside.

Look at the permission column too. drwxr-xr-x means only the owner can write, while the group and others can only read (755 in octal). You can check how to read this notation with the chmod calculator. As explained below, whether you can write is what decides the fix.

Adding safe.directory does not help if you cannot write

If you register the exception exactly as the message says, read operations start working.

$ git config --global --add safe.directory /srv/app
$ git log --oneline
1c877f7 初期設定
$ git pull
error: cannot open '.git/FETCH_HEAD': Permission denied

(The commit messages in the test repository are in Japanese; 初期設定 means “initial setup”.)

git pull writes what it fetched to .git/FETCH_HEAD and rewrites files in the working tree. deploy has no write permission on /srv/app, so it stops here. safe.directory only turns off the owner check. It does not change file permissions.

In this situation, what needs fixing is not safe.directory but how the server is run. If you want to update a directory owned by www-data from deploy, you have two options.

Run Git as the owning user. If you use sudo -u to act as the owner, the owner matches and you can write.

$ sudo -u www-data git -C /srv/app pull
From git://127.0.0.1/origin
   1c877f7..a7771e7  main       -> origin/main
Updating 1c877f7..a7771e7
Fast-forward
 .env.example | 1 +
 1 file changed, 1 insertion(+)

Newly written files are also owned by www-data, so later updates do not end up with mixed owners. Note that the credentials for the remote (SSH keys and so on) are the ones belonging to www-data.

Give ownership to the user who updates. If the web server only needs to read the files, a common split is to have deploy own the code and allow www-data to read only. Move ownership with sudo chown -R deploy:deploy /srv/app and make sure the permissions let the web server read (for example 755 for directories and 644 for files). If there are directories the web server writes to, such as upload or cache directories, give only those back to www-data.

safe.directory fits repositories shared through a group

If several people are meant to update the same repository and you have given a group write permission, safe.directory works as is. Git’s documentation also describes safe.directory as an exception for “repositories you share on purpose”.

As an example, I put deploy and www-data into the group webdev and made /srv/app writable by the group.

sudo groupadd webdev
sudo usermod -aG webdev deploy
sudo usermod -aG webdev www-data
sudo chgrp -R webdev /srv/app
sudo chmod -R g+rwX /srv/app
sudo find /srv/app -type d -exec chmod g+s {} +
sudo -u www-data git -C /srv/app config core.sharedRepository group

The last two lines make files and directories created later also belong to the group webdev and stay writable by the group. chmod g+s sets setgid on the directories, and core.sharedRepository group makes the files Git creates inside .git group-writable. For a new repository, git init --shared=group gives the same setting. Group membership takes effect from the next login.

The owner is still www-data, so from deploy’s point of view it is still someone else’s repository. The group settings alone give the same error.

$ ls -ld /srv/app
drwxrwsr-x 3 www-data webdev 4096 Nov 26 23:53 /srv/app
$ git pull -q
fatal: detected dubious ownership in repository at '/srv/app'
To add an exception for this directory, call:

	git config --global --add safe.directory /srv/app

Register safe.directory now, and since writing is allowed this time, git pull runs all the way through.

$ git config --global --add safe.directory /srv/app
$ git pull -q
$ git log --oneline -1
a7771e7 ログレベルを追加

(ログレベルを追加 means “add log level”.)

When a repository is shared through a group, each user who updates it registers safe.directory in their own configuration.

Permission denied after sudo git pull

Even in your own repository, sudo git pull leaves a different problem behind. Git run with sudo runs as root, but current Git looks at the SUDO_UID environment variable that sudo sets and accepts the repository if it is owned by the original user. This exists so that usage like make && sudo make install does not break, and it is why no error appears.

However, files written as root are owned by root.

$ sudo git pull -q
$ ls -la
drwxrwxr-x 3 deploy deploy 4096 Nov 26 23:52 .
-rw-r--r-- 1 root   root     34 Nov 26 23:52 .env.example
drwxrwxr-x 8 deploy deploy 4096 Nov 26 23:52 .git

The next update without sudo cannot overwrite the .git/FETCH_HEAD that root created, and stops.

$ git pull
error: cannot open '.git/FETCH_HEAD': Permission denied
$ ls -l .git/FETCH_HEAD .git/ORIG_HEAD
-rw-r--r-- 1 root root 82 Nov 26 23:52 .git/FETCH_HEAD
-rw-r--r-- 1 root root 41 Nov 26 23:52 .git/ORIG_HEAD

What you need here is sudo chown -R deploy:deploy <repository> to put the owner back; adding safe.directory will not fix it. If you added sudo to get around a permission error, fix that cause first (for example, a repository placed in a directory you cannot write to).

SUDO_UID also remains when you enter a root shell with sudo -i and use Git there. On the other hand, if the environment is rebuilt, as with sudo su -, or if Git runs as root from the start under cron or as a service, there is no information about the original user, so the repository is refused as someone else’s.

$ sudo env -u SUDO_UID git status
fatal: detected dubious ownership in repository at '/home/deploy/site'

Pitfalls in how you write safe.directory

Even after deciding to use safe.directory, there are a few easy places to trip up.

Register it in your own global configuration. safe.directory only takes effect in --global (~/.gitconfig), --system, or -c on the command line. Writing it in the repository’s .git/config is ignored. It would be pointless if the configuration of a repository someone else created could turn off the check. The global configuration is per user, so registering it as deploy has no effect on Git run as root or www-data.

Write the path exactly as the message shows it. In Git 2.43.0, registering /srv/app/ with a trailing slash did not match, and the error continued. Copying the path shown in the message as is is the safe way. For a repository you reached through a symbolic link, also register the real path shown in the message.

Allowing everything under a directory with /* needs Git 2.46 or later. Git 2.46.0 (July 2024) made it possible to allow every repository under a directory by ending the path with /*, as in /srv/*. In 2.43.0, writing /srv/* did not match. In environments with an older version, such as the standard Ubuntu 24.04 package, register each repository separately.

* turns the check off entirely. Register safe.directory '*' and the error no longer appears for any repository. It is quick, so you see it often in search results, but it removes the protection against the vulnerability altogether. Avoid it on any machine where you might use Git in places others can write to. Limit it to environments where you know there are no other users, such as a throwaway container.

You can check and remove what you registered like this. The second argument to --unset is treated as a regular expression, so wrap it in ^ and $ when you want an exact match.

$ git config --global --get-all safe.directory
/srv/app
$ git config --global --unset safe.directory '^/srv/app$'
$ git config --global --unset-all safe.directory

Remove any * you registered as a test, and any paths you no longer need, here.

If the remote is a local path, it happens on the remote too

If your remote is a local path like /srv/origin.git, or a bare repository on a shared folder mounted over NFS or similar, Git reads the remote repository directly as well. So you get the same error when the remote’s owner is different.

$ sudo -u www-data git -C /srv/app pull
fatal: detected dubious ownership in repository at '/srv/origin.git'
To add an exception for this directory, call:

	git config --global --add safe.directory /srv/origin.git
fatal: Could not read from remote repository.

The clue is that the path in the message is the remote, not the repository you are working in. For a shared bare repository, make it group-writable as in the group-sharing case and have each user register safe.directory. Remotes reached over SSH or HTTPS are read by Git on the server as a user on that server, so local ownership does not matter.

When it shows up in Docker, CI or on Windows

The examples so far are Linux servers, but the same error is common in other environments too. In every case, the cause is a mismatch between the owner and the user running Git.

Docker containers. If you mount a repository cloned on the host (owned by the host user, UID 1000 or similar) into a container and use Git inside the container as root, the owners differ and you get the error. Start the container with --user "$(id -u):$(id -g)" to run with the same UID as the host; the owner then matches, and files the container creates also belong to the host user. For a throwaway container that needs to run as root, you can also register safe.directory inside that container only.

GitHub Actions. It can appear when jobs run in a container, among other cases. By default, actions/checkout registers the checked-out path in safe.directory (input set-safe-directory, default true). If your own script clones somewhere else, or the job switches users partway through, register that path for that user yourself.

Windows. Files on NTFS have owners too, and you get the same error in folders created by another account or by a tool run as administrator. Git for Windows also shows the owner and the current user in the message, so you can see the mismatch there. For a folder you use yourself, the real fix is to open the folder’s Properties in File Explorer and change the owner to yourself under Security → Advanced. For a repository on a file system that does not record owners, such as a USB drive formatted with FAT32 or exFAT, you cannot fix the owner, so you register it with safe.directory. In both cases, register the path in the slash-separated form shown in the message, such as C:/work/app.

Which one to choose

When the error appears, first compare the owner and yourself with ls -ld <repository> and id -un. Then choose like this.

  • It is your repository, but the owner has become root or similar (after sudo git, or after extracting or copying with sudo) → give it back to yourself with chown.
  • You want to update a repository managed by another user on that user’s behalf → run Git as the owner with sudo -u <owner> git ....
  • A shared repository set up to be group-writable, or a drive that does not record owners → register the path, exactly as the message shows it, in safe.directory.

safe.directory is an exception for when you have write permission and know the repository belongs to someone else. If you check the owner once before pasting the command from the message, you avoid struggling with Permission denied, and you avoid switching off the safety mechanism with *.

Recreate the situation in the article and try it (bash, Git 2.43.0, needs root)

Run this as root on a Linux test machine or in a container. It creates the deploy user and the group webdev, and makes repositories under /srv. The remote is published at git://127.0.0.1/ with git daemon.

# Setup: users, remote, working repository
useradd -m -s /bin/bash deploy
export GIT_AUTHOR_NAME=ixam GIT_AUTHOR_EMAIL=ixam@example.com
export GIT_COMMITTER_NAME=ixam GIT_COMMITTER_EMAIL=ixam@example.com
cd /srv && git init -q --bare -b main origin.git && git clone -q origin.git app
cd app && printf 'APP_ENV=production\n' > .env.example && git add .
GIT_AUTHOR_DATE=2026-11-20T10:00:00+09:00 GIT_COMMITTER_DATE=2026-11-20T10:00:00+09:00 git commit -qm "初期設定"
git push -q origin main
git clone -q /srv/origin.git /tmp/up && cd /tmp/up
printf 'APP_ENV=production\nLOG_LEVEL=info\n' > .env.example
GIT_AUTHOR_DATE=2026-11-21T10:00:00+09:00 GIT_COMMITTER_DATE=2026-11-21T10:00:00+09:00 git commit -qam "ログレベルを追加"
git push -q origin main
git daemon --base-path=/srv --export-all --reuseaddr --listen=127.0.0.1 --detach
git -C /srv/app remote set-url origin git://127.0.0.1/origin.git
chown -R www-data:www-data /srv/app

# A repository you cannot write to: adding safe.directory still does not let you write
su - deploy -c 'cd /srv/app && git pull'
su - deploy -c 'git config --global --add safe.directory /srv/app && cd /srv/app && git pull'
sudo -u www-data git -C /srv/app pull

# Sharing through a group (go back one commit first, then try)
su - deploy -c 'git config --global --unset-all safe.directory'
groupadd -f webdev && usermod -aG webdev deploy && usermod -aG webdev www-data
cd /srv/app && chgrp -R webdev . && chmod -R g+rwX . && find . -type d -exec chmod g+s {} +
sudo -u www-data git config core.sharedRepository group
sudo -u www-data git reset -q --hard HEAD~1
su - deploy -c 'cd /srv/app && git pull -q'
su - deploy -c 'git config --global --add safe.directory /srv/app && cd /srv/app && git pull -q && git log --oneline -1'

# sudo git pull leaves root-owned files behind
echo 'deploy ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/deploy
su - deploy -c 'git clone -q git://127.0.0.1/origin.git site && cd site && git reset -q --hard HEAD~1 && sudo git pull -q && ls -la'
su - deploy -c 'cd site && git pull'
su - deploy -c 'cd site && sudo env -u SUDO_UID git status'

The commit dates are fixed, so the hashes match the ones in the article (keep the Japanese commit messages as they are, since they are part of what is hashed). When you are done, delete /etc/sudoers.d/deploy and stop the git daemon process to clean up.

For other Git errors that trip people up, I wrote about git push being rejected in git push rejected (fetch first), and about getting back commits lost with git reset --hard in How to undo git reset –hard. Problems caused by differences in the running user and environment, where a script works when you run it by hand but not from cron, are covered in Shell script works manually but not in cron.