Gitのdubious ownershipエラーを直す方法
fatal: detected dubious ownership in repository は、自分以外のユーザーが持ち主になっているリポジトリで git status や git log を実行したときに出るエラーです。別のユーザーや root で clone したフォルダー、ほかの人と共有しているフォルダーで起きます。エラーの案内どおり git config --global --add safe.directory <リポジトリのパス> を実行するか、フォルダーの持ち主を自分に変えると、同じコマンドが通るようになります。検証環境で実際にエラーを起こし、直したあとの出力もそのまま載せています。
検証環境は Ubuntu 24.04、Git 2.43.0 です。ユーザー名は例示用の値で、リポジトリの持ち主が tama、git コマンドを実行するのが neko です。リポジトリのパス /srv/shared/app も、検証のために作ったものです。ホームフォルダーは毎回空の状態から始め、システム全体の Git 設定は読まない状態で試しました。Windows・macOS、Git for Windows、sudo を付けて git を実行したとき、システム全体の設定(git config --system)に safe.directory を書いたときの動きは試していません。
エラーの全文と、出る条件
リポジトリ /srv/shared/app は、フォルダーも中の .git も、持ち主が tama です。
$ whoami
neko
$ ls -ld /srv/shared/app /srv/shared/app/.git | cut -d" " -f1,3,4,9
drwxrwxr-x tama tama /srv/shared/app
drwxrwxr-x tama tama /srv/shared/app/.git
権限は rwxrwxr-x なので、neko もフォルダーの中を読めます。それでも、neko でこのリポジトリに入って git status を実行すると、次のエラーが出て止まりました。
$ git status; echo "exit=$?"
fatal: detected dubious ownership in repository at '/srv/shared/app'
To add an exception for this directory, call:
git config --global --add safe.directory /srv/shared/app
exit=128
1行目は「/srv/shared/app のリポジトリは、持ち主が怪しい(dubious)」という意味です。そのあとの英文は「このフォルダーを例外にするには、次のコマンドを実行してください」という案内で、実行すべきコマンドがパスまで入った形で書かれています。
git log でも同じエラーになりました。リポジトリの中の src フォルダーで実行しても、エラーに出るパスは src ではなく、リポジトリのいちばん上のフォルダー /srv/shared/app です。
$ pwd
/srv/shared/app/src
$ git status; echo "exit=$?"
fatal: detected dubious ownership in repository at '/srv/shared/app'
To add an exception for this directory, call:
git config --global --add safe.directory /srv/shared/app
exit=128
このチェックは、CVE-2022-24765 という脆弱性への対策として入ったものです。Git 2.36 のリリースノートには、ほかのユーザーの設定ファイルやフックの影響を受けないように、ほかのユーザーが持ち主のリポジトリを認識しないようにした、と書かれています。safe.directory の公式の説明にあるとおり、Git は既定では、自分の持ち物でないリポジトリの設定を読むことも、フックを実行することもしません。
safe.directory にリポジトリを登録して直す
エラーの案内のコマンドを、そのまま neko で実行します。
$ git config --global --add safe.directory /srv/shared/app
$ cat ~/.gitconfig
[safe]
directory = /srv/shared/app
--global を付けたので、neko のホームフォルダーの ~/.gitconfig に書き込まれました。同じ git status と git log をやり直すと、エラーは出なくなりました。
$ git status; echo "exit=$?"
On branch main
nothing to commit, working tree clean
exit=0
$ git log --oneline; echo "exit=$?"
5e4e768 最初のコミット
exit=0
リポジトリの中の src フォルダーで実行した git status --short も、終了コード 0 で通りました。いちばん上のフォルダーを1つ登録すれば、中のサブフォルダーで実行するときにも効きます。
safe.directory は、--add で何個でも追加できます。リポジトリが複数あるときは、それぞれのパスを追加します。
登録をやめたいとき
登録した値は、git config --global --unset-all safe.directory で消せます。消したあとは、また同じエラーが出るようになりました。
$ git config --global --unset-all safe.directory; echo "exit=$?"
exit=0
$ git config --global --get-all safe.directory; echo "exit=$?"
exit=1
$ git status; echo "exit=$?"
fatal: detected dubious ownership in repository at '/srv/shared/app'
To add an exception for this directory, call:
git config --global --add safe.directory /srv/shared/app
exit=128
登録しても消えない書き方
safe.directory は、書く場所とパスの書き方を間違えると効きません。検証環境で試したところ、エラーが消えなかったのは次の3つの書き方です。

1つ目は、リポジトリの中の .git/config に書いたときです。持ち主の tama が git config --local で書き込んでも、neko には同じエラーが出ました。
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[safe]
directory = /srv/shared/app
$ git status; echo "exit=$?"
fatal: detected dubious ownership in repository at '/srv/shared/app'
To add an exception for this directory, call:
git config --global --add safe.directory /srv/shared/app
exit=128
git config の公式の説明によると、safe.directory はシステム全体の設定(--system)、ユーザーの設定(--global)、コマンドラインで渡した設定(git -c)の3か所でしか読まれません。リポジトリの中の設定で自分を安全だと名乗れてしまうと、チェックの意味がなくなるからです。
2つ目は、エラーが出たサブフォルダーのパスを登録したときです。src で作業していても、登録するのはエラーの at '…' に出たいちばん上のフォルダーです。
$ git config --global --add safe.directory /srv/shared/app/src
$ git config --global --get-all safe.directory
/srv/shared/app/src
$ git status; echo "exit=$?"
fatal: detected dubious ownership in repository at '/srv/shared/app'
To add an exception for this directory, call:
git config --global --add safe.directory /srv/shared/app
exit=128
3つ目は、パスの最後に / を付けたときです。Git 2.43.0 では、/srv/shared/app/ と登録しても /srv/shared/app とは別のパスとして扱われ、エラーは消えませんでした。
$ git config --global --add safe.directory /srv/shared/app/
$ git status; echo "exit=$?"
fatal: detected dubious ownership in repository at '/srv/shared/app'
To add an exception for this directory, call:
git config --global --add safe.directory /srv/shared/app
exit=128
どれも、エラーの最後の行に出ているコマンドをそのまま写せば避けられます。
1回だけ通したいときは git -c を使う
設定ファイルに残したくないときは、git -c safe.directory=<パス> で、そのコマンドだけ通せます。次のコマンドではまた同じエラーが出ます。
$ git -c safe.directory=/srv/shared/app log --oneline; echo "exit=$?"
5e4e768 最初のコミット
exit=0
$ git status; echo "exit=$?"
fatal: detected dubious ownership in repository at '/srv/shared/app'
To add an exception for this directory, call:
git config --global --add safe.directory /srv/shared/app
exit=128
自分のリポジトリなら、持ち主を自分に変える
root や別のユーザーで clone してしまったなど、本来は自分のリポジトリであるときは、safe.directory を使わずに持ち主を自分に変える方法もあります。root で chown -R を実行し、持ち主を neko に変えました。safe.directory は何も登録していない状態です。
$ chown -R neko:neko /srv/shared/app
$ ls -ld /srv/shared/app /srv/shared/app/.git | cut -d" " -f1,3,4,9
drwxrwxr-x neko neko /srv/shared/app
drwxrwxr-x neko neko /srv/shared/app/.git
$ git config --global --get-all safe.directory; echo "exit=$?"
exit=1
$ git status; echo "exit=$?"
On branch main
nothing to commit, working tree clean
exit=0
持ち主が自分になったので、チェックにかからなくなりました。ただし、Git は既定で、実行したユーザー自身が持ち主のリポジトリしか扱いません。ほかのユーザーも使っている共有のリポジトリで持ち主を変えると、今度はそのユーザーがこのチェックにかかります。safe.directory の説明でも、この設定の使いどころとして、意図して共有しているリポジトリを挙げています。
safe.directory に * を入れるとチェックが止まる
safe.directory に * を入れると、どのリポジトリも登録済みとして扱われます。検証環境でも、登録していない別のリポジトリ /srv/shared/other まで、エラーなしで git log が通りました。
$ git config --global --add safe.directory '*'
$ cat ~/.gitconfig
[safe]
directory = *
$ pwd
/srv/shared/other
$ git log --oneline; echo "exit=$?"
5e4e768 最初のコミット
exit=0
公式ドキュメントの safe.directory の項にあるとおり、* はこのセキュリティチェックを完全にやめる設定です。ほかのユーザーが持ち主のリポジトリの設定やフックも、自分のものと同じように使われるようになります。エラーを消すためだけに * を入れず、信頼できるリポジトリのパスを1つずつ登録してください。



