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

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


当前回答

我遇到这个问题太多次了,但每次发生时,我都尝试使用sudo命令推送或提交,在输入密码后,我不使用sudo推送或提交 例如

sudo git commit -m "message"

然后再输入密码

git commit -m "message"

其他回答

也有可能您添加了另一个具有相同别名的本地存储库。例如,您现在有两个本地文件夹称为origin,因此当您尝试推送时,远程存储库将不接受您的凭据。

重命名本地存储库别名,您可以通过这个链接https://stackoverflow.com/a/26651835/2270348

也许您可以留下一个您喜欢的本地存储库作为origin,而其他存储库则重命名它们,例如从origin到另一个origin。记住,这些只是别名,你所需要做的就是记住新的别名和它们各自的远程分支。

修复权限

我用这个来修复我的。git文件夹,@richard-hansen的答案缺少用户。

首先,您需要进入.git文件夹。

cd /path/to/repo/.git

然后执行这些命令。

sudo chown -R user:groupname .
sudo chmod -R g+rwX .
sudo find . -type d -exec chmod g+s '{}' +

这也将修复所有子模块。

在你添加了一些东西之后……提交它们,并在所有完成后推动它!爆炸! !开始所有的问题…正如您应该注意到的,在定义新项目和现有项目的方式上存在一些差异。如果其他人尝试添加/提交/推送相同的文件或内容(git将两者保持为相同的对象),我们将面临以下错误:

$ git push
Counting objects: 31, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (21/21), 2.07 KiB | 0 bytes/s, done.
Total 21 (delta 12), reused 0 (delta 0)
remote: error: insufficient permission for adding an object to repository database ./objects  remote: fatal: failed to write object

要解决这个问题,您必须考虑到操作系统的权限系统,因为在这种情况下您受到了它的限制。为了更好地理解这个问题,继续检查你的git对象的文件夹(.git/objects)。你可能会看到这样的东西:

<your user_name>@<the machine name> objects]$ ls -la
total 200
drwxr-xr-x 25 <your user_name> <group_name> 2048 Feb 10 09:28 .
drwxr-xr-x  3 <his user_name> <group_name> 1024 Feb  3 15:06 ..
drwxr-xr-x  2 <his user_name> <group_name> 1024 Jan 31 13:39 02
drwxr-xr-x  2 <his user_name> <group_name> 1024 Feb  3 13:24 08

*注意,这些文件的权限只授予你的用户,没有人可以改变它…*

Level       u   g   o
Permission rwx r-x ---
Binary     111 101 000
Octal       7   5   0

解决问题

如果你有超级用户权限,你可以继续使用第二步自己更改所有权限,在任何其他情况下,你将需要询问所有用户与他们的用户创建的对象,使用下面的命令来知道他们是谁:

$ ls -la | awk '{print $3}' | sort -u 
<your user_name>
<his user_name>

现在你和所有文件的所有者用户将不得不改变这些文件的权限,做:

$ chmod -R 774 .

之后,你需要添加一个新属性,等价于——shared=group,根据文档,这使存储库组可写,执行:

$ git config core.sharedRepository group

https://coderwall.com/p/8b3ksg

修复权限

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

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.

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

该问题是由于以根用户身份运行某些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"