Git“致命:在存储库中检测到可疑的所有权”

bil*_*gic 4 cron git

Description:    Ubuntu 22.04.1 LTS
Release:        22.04
Run Code Online (Sandbox Code Playgroud)

我试图在用户下执行一些 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
Run Code Online (Sandbox Code Playgroud)

仍然遇到同样的错误。有什么想法吗?谢谢。

bil*_*gic 6

Git 要求当前用户是文件的所有者,如果当前用户是组的“成员”,则不起作用

对于.git属于以下的文件user1:group1

  • 如果你是这样的话,Git 不会抛出错误user1
  • 如果你是这样,Git 会抛出错误user2:group1

解决方案 1(最佳)

由于这是自动化的/cron,因此只需为要运行的用户添加前缀会更容易。在git命令前指定用户

sudo -u <user> git ...

解决方案2

稍微麻烦一点,因为您需要将每个存储库添加到user.gitconfig并且这里有一个非常详细的答案/sf/answers/5117015991/

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

Run Code Online (Sandbox Code Playgroud)

我希望这足够清楚,否则请询问或评论,我会修改这个答案。