当我尝试推到共享git远程时,我得到以下错误: 向存储库数据库添加对象权限不足

这适用于下一次推送,因为所有的文件都属于正确的组,但下一次有人推送一个更改时,它会在对象文件夹中创建一个新项目,该项目的默认组为组。我唯一能想到的就是改变所有开发人员签入项目的默认组,但这似乎是一种hack。什么好主意吗?谢谢。


当前回答

我遇到了同样的问题。阅读这里,我意识到这是文件权限的消息是指。对我来说,解决办法是:

/etc/inetd.d/git-gpv

它以“nobody”用户启动git-daemon,因此缺乏写权限。

# Who   When    What
# GPV   20Nov13 Created this by hand while reading: http://linuxclues.blogspot.co.uk/2013/06>/git-daemon-ssh-create-repository-debian.html
# GPV   20Nov13 Changed owner (to user_git) otherise nobody lack permission to update the repository
#git stream tcp nowait nobody  /usr/bin/git git daemon --inetd --verbose --enable=receive-pack --export-all /gitrepo
git stream tcp nowait user_git  /usr/bin/git git daemon --inetd --verbose --enable=receive-pack --export-all /gitrepo

(我怀疑其他人把他们的inetd conf文件称为git-gpv。通常它会直接在/etc/inetd.conf中)

其他回答

我将补充我的意见,作为一种发现目录中具有特定所有权的文件的方法。

该问题是由于以根用户身份运行某些git命令引起的。 收到的信息是:

$ git commit -a -m "fix xxx"
error: insufficient permission for adding an object to repository database .git/objects
error: setup.sh: failed to insert into database

我首先查看了git config -l,然后我解决了:

find .git/ -exec stat --format="%G %n" {} + |grep root

chown -R $(id -un):$(id -gn) .git/objects/

git commit -a -m "fixed git objects ownership"

修复权限

在你确定并修复了潜在的原因(见下文)后,你会想要修复权限:

cd /path/to/repo/.git
sudo chgrp -R groupname .
sudo chmod -R g+rwX .
sudo find . -type d -exec chmod g+s '{}' +

注意,如果你想让每个人都能修改存储库,你不需要chgrp,你需要将chmod更改为sudo chmod -R a+rwX。

如果您不修复根本原因,错误将不断出现,您将不得不一遍又一遍地重新运行上述命令。

根本原因

该错误可能是由以下原因之一引起的:

The repository isn't configured to be a shared repository (see core.sharedRepository in git help config). If the output of: git config core.sharedRepository is not group or true or 1 or some mask, try running: git config core.sharedRepository group and then re-run the recursive chmod and chgrp (see "Repair Permissions" above). The operating system doesn't interpret a setgid bit on directories as "all new files and subdirectories should inherit the group owner". When core.sharedRepository is true or group, Git relies on a feature of GNU operating systems (e.g., every Linux distribution) to ensure that newly created subdirectories are owned by the correct group (the group that all of the repository's users are in). This feature is documented in the GNU coreutils documentation: ... [If] a directory's set-group-ID bit is set, newly created subfiles inherit the same group as the directory, and newly created subdirectories inherit the set-group-ID bit of the parent directory. ... [This mechanism lets] users share files more easily, by lessening the need to use chmod or chown to share new files. However, not all operating systems have this feature (NetBSD is one example). For those operating systems, you should make sure that all of your Git users have the same default group. Alternatively, you can make the repository world-writable by running git config core.sharedRepository world (but be careful—this is less secure). The file system doesn't support the setgid bit (e.g., FAT). ext2, ext3, ext4 all support the setgid bit. As far as I know, the file systems that don't support the setgid bit also don't support the concept of group ownership so all files and directories will be owned by the same group anyway (which group is a mount option). In this case, make sure all Git users are in the group that owns all the files in the file system. Not all of the Git users are in the same group that owns the repository directories. Make sure the group owner on the directories is correct and that all users are in that group.

只需复制并粘贴到您自己的终端。

sudo chown -R "${USER:-$(id -un)}" .

我只是尝试了sudo git commit -m“XY”,然后我用CTRL + C取消了它,然后再次尝试git commit -m“XY”,然后它突然工作了。

调试这种情况的一个好方法是,下次发生这种情况时,SSH到远程repo, cd到objects文件夹,并执行ls -al。

如果您看到2-3个文件具有不同的用户:组所有权,那么这就是问题所在。

在过去,我遇到过一些遗留脚本访问我们的git repo,通常意味着不同的(unix)用户最后推送/修改文件,而您的用户没有权限覆盖这些文件。你应该创建一个共享git组,所有启用git的用户都在其中,然后递归地chgrp objects文件夹和它的内容,这样它的组所有权就是共享git组。

您还应该在文件夹上添加一个sticky位,以便在文件夹中创建的所有文件将始终具有git组。

Chmod g+s目录名

更新:我不知道core. sharerepository。很高兴知道这一点,尽管它可能只是做上述工作。