Git“致命:在儲存庫中偵測到可疑的所有權”

Git“致命:在儲存庫中偵測到可疑的所有權”
Description:    Ubuntu 22.04.1 LTS
Release:        22.04

我試圖在使用者下執行一些 git 命令www-data,但它不起作用。

為了排除故障,我嘗試運行sudo -u www-data git show但它返回fatal: detected dubious ownership in repository

如果我運行sudo -u www-data git diff --shortstat它會返回warning: Not a git repository. Use --no-index to compare two paths outside a working tree

我也嘗試添加到sudoers

www-data ALL=(ALL) NOPASSWD: /usr/bin/git show

# and also

www-data ALL=(ALL) NOPASSWD: /usr/bin/git

仍然遇到同樣的錯誤。有什麼想法嗎?謝謝。

答案1

Git 要求目前使用者是文件的擁有者,如果目前使用者是群組的“成員”,則不起作用

對於.git屬於以下的文件user1:group1

  • 如果你是這樣的話,Git 不會拋出錯誤user1
  • 如果你是這樣,Git 會拋出錯誤user2:group1

解決方案 1(最佳)

由於這是自動化的/cron,因此只需為要運行的用戶添加前綴會更容易。在git指令前指定用戶

sudo -u <user> git ...

解決方案2

稍微麻煩一點,因為您需要將每個存儲庫添加到user's中.gitconfig,並且這裡有一個非常詳細的答案https://stackoverflow.com/a/73100228/3872647

[safe]
    directory = <real path of git repository, symlink won't work here>

我希望這足夠清楚,否則請詢問或評論,我會修改這個答案。

答案2

+1 表示“git 要求目前使用者是文件的擁有者”

  • 我目前的 git 版本是 2.43.1,此錯誤訊息是新的 ??.?.?
  • 設定 safe.directories 沒有執行任何操作
  • 我正在使用的腳本開頭sudo -u www-data bash -c '...',我發現確實至少 .git 和孩子需要擁有www-data

答案3

所以,我發現如果您編輯用戶資料夾中的 .gitconfig 檔案(應該類似於/home/your_user/.gitconfig)並插入: 錯誤將不再出現。
[safe]
directory = /path/to/directory

fatal: detected dubious ownership in repository

相關內容