Git 推送错误:没有足够的权限将对象添加到存储库数据库
当我尝试推送到共享 git 远程时,出现以下错误:
向存储库数据库添加对象的权限不足
然后我在这里阅读了有关修复的信息:修复< /a> 这适用于下一次推送,因为所有文件都属于正确的组,但下次有人推送更改时,它会在对象文件夹中创建一个新项目,并将其默认组作为该组。我唯一能想到的就是更改开发人员签入项目的所有默认组,但这似乎是一种黑客行为。有什么想法吗?谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
修复权限
确定并修复根本原因(见下文)后,您需要修复权限:
请注意,如果您希望每个人都能够修改存储库,则不需要
chgrp 并且您需要将 chmod 更改为
sudo chmod -R a+rwX 。
如果您不解决根本原因,错误将不断出现,您必须不断重新 -运行以上命令并再来一次。
根本原因
该错误可能由以下原因之一引起:
存储库未配置为共享存储库(请参阅
git help config
中的core.sharedRepository
)。如果输出:不是
group
或true
或1
或某些掩码,请尝试运行:然后重新运行递归
chmod
和chgrp
(请参阅上面的“修复权限”)。操作系统不会将目录上的 setgid 位解释为“所有新文件和子目录应继承组所有者”。
当
core.sharedRepository
为true
或group
时,Git依赖GNU操作系统(例如,每个Linux发行版)的功能来确保新创建的子目录属于正确的组(所有存储库用户所在的组)。此功能记录在 GNU coreutils 文档:<块引用>
... [如果]设置了目录的 set-group-ID 位,则新创建的子文件将继承与该目录相同的组,并且新创建的子目录将继承父目录的 set-group-ID 位。 ...[此机制让]用户可以更轻松地共享文件,减少使用
chmod
或chown
共享新文件的需要。但是,并非所有操作系统都具有此功能(NetBSD 就是一个例子)。对于这些操作系统,您应该确保所有 Git 用户都具有相同的默认组。或者,您可以通过运行 git config core.sharedRepository world 使存储库可全局写入(但要小心 - 这不太安全)。
文件系统不支持setgid 位(例如FAT)。 ext2、ext3、ext4 都支持 setgid 位。据我所知,不支持 setgid 位的文件系统也不支持组所有权的概念,因此所有文件和目录无论如何都将属于同一组(哪个组是挂载选项)。在这种情况下,请确保所有 Git 用户都位于拥有文件系统中所有文件的组中。
并非所有 Git 用户都位于拥有存储库目录的同一组中。确保目录的组所有者正确并且所有用户都在该组中。
Repair Permissions
After you have identified and fixed the underlying cause (see below), you'll want to repair the permissions:
Note if you want everyone to be able to modify the repository, you don't need the
chgrp
and you will want to change the chmod tosudo chmod -R a+rwX .
If you do not fix the underlying cause, the error will keep coming back and you'll have to keep re-running the above commands over and over again.
Underlying Causes
The error could be caused by one of the following:
The repository isn't configured to be a shared repository (see
core.sharedRepository
ingit help config
). If the output of:is not
group
ortrue
or1
or some mask, try running:and then re-run the recursive
chmod
andchgrp
(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
istrue
orgroup
, 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: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.
对于 Ubuntu(或任何 Linux)
,从项目根目录,
您可以通过以下方式告诉您的名字和您的组:
注意:记住 sudo 行末尾的星号
For Ubuntu (or any Linux)
From project root,
You can tell yourname and yourgroup by:
Note: remember the star at the end of the sudo line
使用以下命令,就像魔术一样
准确地键入命令(末尾有额外的空格和一个点)
的命令细分
以 root 用户身份运行
更改所有权
对所有文件和文件夹进行递归操作
从 $USER 获取用户名并如果未设置,则通过运行 id -un 获取值
定位当前目录
use the following command, works like magic
type the command exactly as it is (with extra spaces and one dot at the end)
breakdown of command
run as root user
change ownership
Recursive action against all files and folders
Get the user name from $USER and if that is not set get the value from running
id -un
Target the current directory
sudo chmod -R ug+w .;
基本上,
.git/objects
文件没有写入权限。上面的行授予目录中所有文件和文件夹的权限。sudo chmod -R ug+w .;
Basically,
.git/objects
file does not have write permissions. The above line grants permission to all the files and folders in the directory.我只是想添加我的解决方案。我在 OS X 上有一个存储库,它在某些目录上拥有 root 所有权,在其他目录上拥有 Home(这是我的用户目录),这导致了上面列出的相同错误。
谢天谢地,解决方案很简单。从航站楼出发:
I just wanted to add my solution. I had a repo on OS X that had ownership of root on some directories and Home (which is my user directory) on others which caused the same error listed above.
The solution was simple thankfully. From terminal:
调试此问题的一个好方法是下次发生这种情况时,通过 SSH 进入远程存储库,cd 进入对象文件夹并执行
ls -al
。如果您看到 2-3 个文件具有不同的用户:组所有权,那么这就是问题所在。
我过去曾遇到过这样的情况,一些遗留脚本访问我们的 git 存储库,通常意味着不同的(unix)用户最后推送/修改了文件,而您的用户没有覆盖这些文件的权限。您应该创建一个所有启用 git 的用户都在其中的共享 git 组,然后递归地
chgrp
objects
文件夹及其内容,以便它的组所有权是共享git
组。您还应该在文件夹上添加一个粘滞位,以便在该文件夹中创建的所有文件将始终具有 git 组。
更新:我不知道 core.sharedRepository。很高兴知道,尽管它可能只是执行上述操作。
A good way to debug this is the next time it happens, SSH into the remote repo, cd into the objects folder and do an
ls -al
.If you see 2-3 files with different user:group ownership than this is the problem.
It's happened to me in the past with some legacy scripts access our git repo and usually means a different (unix) user pushed / modified files last and your user doesn't have permissions to overwrite those files. You should create a shared git group that all git-enabled users are in and then recursively
chgrp
theobjects
folder and it's contents so that it's group ownership is the sharedgit
group.You should also add a sticky bit on the folder so that all the files created in the folder will always have the group of
git
.Update: I didn't know about core.sharedRepository. Good to know, though it probably just does the above.
为我解决了...
只是这样:
Solved for me...
just this:
最简单的解决方案是:
从项目目录:
The sumplest solution is:
From the project dir:
只需将其复制并粘贴到您自己的终端中即可。
just copy and paste this into your own terminal.
如果您使用与推送更改时计划使用的用户不同的用户运行 git init,则很容易发生这种情况。
如果您盲目地按照 [1] 上的说明进行操作,就会发生这种情况,因为您可能以 root 身份创建了 git-user,然后立即转到 git init,而没有在其间更改用户。
[1] http://git-scm.com/book /en/Git-on-the-Server-Setting-Up-the-Server
This can easily happen if you ran
git init
with a different user from the one you are planning to use when pushing changes.If you blindly follow the instructions on [1] this will happen as you probably created the git-user as root and then immediately moved on to git init without changing user in between.
[1] http://git-scm.com/book/en/Git-on-the-Server-Setting-Up-the-Server
Linux、macOS:
其中
name
是您的用户名,group
是您的用户名所属的组。Linux, macOS:
where
name
is your username andgroup
is the group that your username belongs to.您需要复制此命令并将其粘贴到终端上:-
sudo chmod 777 -R .git/objects
You need to copy and paste this command on your terminal :-
sudo chmod 777 -R .git/objects
添加一些内容后...提交它们,并在完成后推送它!砰!!开始所有问题...您应该注意到,新项目和现有项目的定义方式存在一些差异。如果其他人尝试添加/提交/推送相同的文件或内容(git 将两者保留为相同的对象),我们将面临以下错误:
要解决此问题,您必须牢记操作系统的权限系统在这种情况下受其限制。如果你更好地理解了这个问题,请继续检查你的 git 对象的文件夹(.git/objects)。您可能会看到类似这样的内容:
*请注意,这些文件的权限仅授予您的用户,没有人永远无法更改它... *
解决问题
如果您拥有超级用户权限,您可以继续更改所有权限您自己使用第二步,在任何其他情况下,您将需要询问所有使用其用户创建的对象的用户,使用以下命令来了解他们是谁:
现在您和所有文件的所有者用户将必须更改这些文件的权限,做:
之后你需要添加一个相当于为新存储库完成的 --shared=group 的新属性,根据文档,这使存储库组可写,执行它:
https://coderwall.com/p/8b3ksg
After you add some stuff... commit them and after all finished push it! BANG!! Start all problems... As you should notice there are some differences in the way both new and existent projects were defined. If some other person tries to add/commit/push same files, or content (git keep both as same objects), we will face the following error:
To solve this problem you have to have something in mind operational system's permissions system as you are restricted by it in this case. Tu understand better the problem, go ahead and check your git object's folder (.git/objects). You will probably see something like that:
*Note that those file's permissions were granted only for your users, no one will never can changed it... *
SOLVING THE PROBLEM
If you have super user permission, you can go forward and change all permissions by yourself using the step two, in any-other case you will need to ask all users with objects created with their users, use the following command to know who they are:
Now you and all file's owner users will have to change those files permission, doing:
After that you will need to add a new property that is equivalent to --shared=group done for the new repository, according to the documentation, this make the repository group-writable, do it executing:
https://coderwall.com/p/8b3ksg
我将添加我的两分钱,作为发现目录中具有特定所有权的文件的一种方式。
该问题是由于以 root 身份运行某些 git 命令引起的。
收到的消息是:
我首先查看了 git config -l ,然后我解决了:
I'll add my two cents just as a way to discover files with a specific ownership inside a directory.
The issue was caused by running some git command as root.
The received message was:
I first looked at
git config -l
, then I resolved with:对于我的情况,所有建议都不起作用。我使用的是 Windows,这对我有用:
git remote add foo //SERVERNAME/path/to/copied/git
)git Push foo master
。有效吗?伟大的!现在删除不工作的存储库并将其重命名为以前的名称。确保权限和共享属性保持不变。For my case none of the suggestions worked. I'm on Windows and this worked for me:
git remote add foo //SERVERNAME/path/to/copied/git
)git push foo master
. Did it worked? Great! Now delete not-working repo and rename this into whatever it was before. Make sure permissions and share property remains the same.修复权限
我用它来修复我的 .git 文件夹,@richard-hansen 的答案缺少用户。
首先,您需要进入 .git 文件夹。
然后执行这些命令。
这也将修复所有子模块。
Repair Permissions
I used this to repair my .git folder, the answer of @richard-hansen was missing the user.
First you need to go into the .git folder.
Then execute these commands.
This will also fix all submodules.
如果您在 git bash、EC2 实例或任何 linux、ubuntu、macOS 系统中遇到此问题。您可以尝试运行:
sudo git add 。
它在 AWS EC2 ubuntu 实例中适用于我。
If you are facing this issue in git bash, EC2 instance or any linux, ubuntu, macOS system. You can try to run:
sudo git add .
It works for me in AWS EC2 ubuntu instance.
我遇到了同样的问题。读到这里我意识到该消息所指的是文件权限。对我来说,修复位于:
/etc/inetd.d/git-gpv
它以用户“nobody”身份启动 git-daemon,因此缺乏写入权限。
(我怀疑其他人会调用他们的 inetd conf 文件 git-gpv 。通常它会直接在 /etc/inetd.conf 中)
I hit this same issue. Reading around here I realised it was file permissions the message was referring to. The fix , for me, was in:
/etc/inetd.d/git-gpv
It was starting git-daemon as user 'nobody' so lacked the write permission.
(I doubt other call their inetd conf file git-gpv . Commonly it would be directly in /etc/inetd.conf)
您需要对要推送到的目录有足够的写入权限。
就我而言:Windows 2008 服务器
右键单击 git repo 目录或父目录。
属性>共享选项卡>高级分享>权限>确保用户具有适当的访问权限。
You need the sufficient write permissions on the directory that you are pushing to.
In my case: Windows 2008 server
right click on git repo directory or parent directory.
Properties > Sharing tab > Advanced Sharing > Permissions > make sure the user has appropriate access rights.
您也有可能添加了具有相同别名的另一个本地存储库。例如,您现在有 2 个称为
origin
的本地文件夹,因此当您尝试推送时,远程存储库将不会接受您的凭据。重命名本地存储库别名,您可以点击此链接 https://stackoverflow.com/a/26651835/2270348
也许您可以将您喜欢的 1 个本地存储库保留为
origin
,其他存储库将其重命名,例如从origin
更改为另一个来源
。请记住,这些只是别名,您所需要做的就是记住新别名及其各自的远程分支。There is a possibility also that you added another local repository with the same alias. As an example, you now have 2 local folders referred to as
origin
so when you try to push, the remote repository will not accept you credentials.Rename the local repository aliases, you can follow this link https://stackoverflow.com/a/26651835/2270348
Maybe you can leave 1 local repository of your liking as
origin
and the others rename them for example fromorigin
toanotherorigin
. Remember these are just aliases and all you need to do is remember the new aliases and their respective remote branches.对我有用
Works for me
对于这个问题还有一个解决方案,当您使用多个正在运行的 docker 容器并尝试更改和提交/推送某些内容时,可以重现该问题。
就我而言,当所有容器都启动时,我无法提交任何内容。但一旦我杀了他们——我就能毫无问题地做出承诺。
我没有太多研究这种行为的原因,但我可以猜测您在本地更改的代码在 docker 容器中重用,并且因为它是从 root 用户运行的,因此它可以更改它所工作的文件的一些权限with - 这可能会导致问题。
There is one more solution to this problem which can be reproduced when you work with multiple running docker containers and try to change and commit/push something.
In my case I could not commit anything while all the containers where up. But as soon as I killed them - I was able to commit without any issues.
I did not study the reason for such a behavior much, but I can guess that the code which you are changing locally is reused in a docker container and as it's being run from root user and thus it can change some permissions of the files it works with - this can cause an issue.
我在加入 Rstudio 项目时得到了这个。我意识到我忘记了:
在程序启动时。事实上,由于我还有另一个错误,我实际上需要这样做:
I got this when pulling into an Rstudio project. I realised I forgot to do:
on program startup. In fact as there's another bug I've got, I need to actually do:
使用git很长时间没有问题,今天遇到了这个问题。经过一番思考,我意识到今天早些时候我将
umask
从022
更改为其他内容。其他人的所有答案都是有帮助的,即对有问题的目录执行 chmod 。但根本原因是我的新 umask ,每当在 .git/object/ 下创建新目录时,它总是会导致新问题。因此,对我来说,长期解决方案是将
umask
更改回022
。After using git for a long time without problems, I encountered this problem today. After some reflection, I realized I changed my
umask
earlier today from022
to something else.All the answers by other people are helpful, i.e., do
chmod
for the offending directories. But the root cause is my newumask
which will always cause a new problem down the road whenever a new directory is created under.git/object/
. So, the long term solution for me is to changeumask
back to022
.我在使用 Vagrant 运行远程开发机器时遇到此错误。上述解决方案均无效,因为所有文件都具有正确的权限。
我通过将 config.vm.box = "hasicorp/bionic64" 更改为
config.vm.box = "bento/ubuntu-20.10"
修复了该问题。I was getting this error when running a remote development development machine using Vagrant. None of the above solutions worked because all the files had the correct permissions.
I fixed it by changing
config.vm.box = "hasicorp/bionic64"
toconfig.vm.box = "bento/ubuntu-20.10"
.就我而言,解决方案很简单,再次
git commit
。问题自动消失了。
发生了什么?我使用 ^C (Control-C) 来避免编写错误的提交消息。 (我从错误的剪贴板粘贴了错误的消息。)所以我假设该进程在后台暂时冻结,从而暂时锁定了数据库。
In my case, the solution was simply to
git commit
again.The problem went away automatically.
What happened? I used ^C (Control-C) to break out of writing a bad commit message. (I pasted the wrong message from the wrong clipboard.) So I assume the process was temporarily frozen in the background, which locked up the database temporarily.
我通过使用基于
ssh://
的 URL 而不是基于http://
的 URL 解决了这个问题。几天前,我已经使用基于
http://
的 URL 克隆了存储库。在克隆和推送之间,我必须在我的帐户上启用 2FA,然后将我的公钥添加到代码存储库中。由于启用了 2FA,
http://
URL 无法正常工作。I got this issue resolved by making use of
ssh://
based URL instead ofhttp://
based URL.I had cloned the repository quite a few days earlier using the
http://
based URL. In between the cloning and pushing, I had to enable 2FA on my account and subsequently get my public key added to the code repository.Due to enabled 2FA the
http://
URL was not working properly.确保以管理员身份打开命令行提示符。然后,确保项目文件不是只读的。
在 Windows 中,您可以通过右键单击项目文件夹 -> 来检查这一点。点击“显示更多选项”->点击“属性”->取消选择“只读”->单击“应用”
Make sure you open the command line prompt as admin. Then, make sure project files are not read-only.
In windows, you can check this by right-clicking on project folder -> click "Show more options" -> click "Properties" -> deselect "Read-only" -> click "Apply"
我刚刚尝试了 sudo git commit -m "XY" ,然后我用 CTRL + C 取消了它,并用 git commit -m "XY" 再次尝试,然后它突然起作用了。
I just tried
sudo git commit -m "XY"
then I canceled it with CTRL + C and tried again withgit commit -m "XY"
then it suddenly worked.最简单最简单的解决方案 ↓
EASIEST & SIMPLEST SOLUTION ↓