无法将文件添加到 Git 仓库,但可以更改/提交。

5

这里出现了错误:

git add .emacs
error: insufficient permission for adding an object to repository database .git/objects

error: .emacs: failed to insert into database
error: unable to index file .emacs
fatal: adding files failed

我该如何修复它?它出了什么问题?

1
请检查.git和.git/objects上的权限,它们是否合理? - NSSec
4个回答

16
error: insufficient permission for adding an object to repository database .git/objects

这就是你的问题所在。由于某种原因,Git无法写入.git/objects - 通常意味着它由另一个用户拥有且没有正确的权限。

尝试通过运行ls -l .git/objects来查看文件夹当前的权限。然后,您可以根据需要使用chmodchown(或两者都)进行更改。


6

我没有改变文件的权限,但是出现了相同的错误信息,通过更改git目录的所有权(chown -R user:group .git)解决了问题。


4
这可能是因为不小心使用sudo提交了git commit引起的。你并没有有意改变权限,sudo在大多数配置中都可以正常工作,但下一次尝试时如果没有使用sudo,你将会遇到问题。
只需使用以下命令将.git目录的权限恢复到之前的状态即可解决问题。
sudo chown -R user:group .git

0
请注意,对于更近期(2014年及以后)的Git版本,当对象存储不可写时,"git commit"(man)会给出重复的错误消息,这在Git 2.34(2021年第四季度)中已得到修正。

查看提交 4ef91a2提交 119b26d(2021年10月12日)由Ævar Arnfjörð Bjarmason (avar)提交。
(由Junio C Hamano -- gitster --合并于提交 2c428e4,2021年10月25日)

commit:修复权限错误输出中的重复回归问题

签名作者:Ævar Arnfjörð Bjarmason

修复了一个回归问题,即在无法写入.git/objects时发出的错误输出。
9c4d6c0 ("cache-tree: Write updated cache-tree after commit", 2014-07-13, Git v2.2.0-rc0 -- merge)之前,我们只会发出一个"权限不足"的错误,现在我们又会这样做了。
原因相当简单,我们有一个WRITE_TREE_SILENT用例,想要静默地准备索引,消除任何权限等错误输出。
然后当我们尝试更新到(可能损坏的)索引时,我们将再次遇到相同的错误。
但是,在9c4d6c0中,缓存树API和对象存储之间的差距在要求write_object_file()保持沉默方面没有被消除。
即,在9c4d6c0之后,第一个调用是prepare_index(),之后我们将调用prepare_to_commit()
我们只想从后者获得详细的错误输出。
因此,让我们添加并使用具有相应HASH_SILENT标志的设施,它的唯一用户是cache-tree.cupdate_one(),如果设置了其"WRITE_TREE_SILENT"标志,则会将其设置。

所以除了需要使用chown来解决问题之外,如果你看到错误信息两次,也不要感到惊讶。


在合并操作中,你也遇到了类似的错误(涉及到add

在只读仓库中,"git merge-tree"(man) 试图生成一个合并结果树对象,但失败了(这是正常的),导致了段错误(这是不好的),这个问题已经在 Git 2.39(2022年第四季度)中得到了修复。

请查看92481d1提交0b55d93提交(2022年9月28日)来自Johannes Schindelin (dscho)
(由Junio C Hamano -- gitster --19118cb提交中合并,于2022年10月10日)

merge-ort:在写入blob失败时提前返回

经过Elijah Newren审核
Johannes Schindelin签署

In the previous commit, we fixed a segmentation fault when a tree object could not be written.

However, before the tree object is written, merge-ort wants to write out a blob object (except in cases where the merge results in a blob that already exists in the database).
And this can fail, too, but we ignore that write failure so far.

Let's pay close attention and error out early if the blob could not be written.
This reduces the error output of t4301.25 ("merge-ort fails gracefully in a read-only repository") from:

error: insufficient permission for adding an object to repository database ./objects
error: error: Unable to add numbers to database
error: insufficient permission for adding an object to repository database ./objects
error: error: Unable to add greeting to database
error: insufficient permission for adding an object to repository database ./objects
fatal: failure to merge

to:

error: insufficient permission for adding an object to repository database ./objects
error: error: Unable to add numbers to database
fatal: failure to merge

This is not just a cosmetic change: Even though one might assume that the operation would have failed anyway at the point when the new tree object is written (and the corresponding tree object will be new if it contains a blob that is new), but that is not so:

As pointed out by Elijah Newren, when Git has previously been allowed to add loose objects via sudo calls, it is very possible that the blob object cannot be written (because the corresponding .git/objects/??/ directory may be owned by root) but the tree object can be written (because the corresponding objects directory is owned by the current user).

This would result in a corrupt repository because it is missing the blob object, and with this here patch we prevent that.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接