为什么我不能在git中提交一个空文件夹? [英] Why can't I commit an empty folder in git?

查看:1043
本文介绍了为什么我不能在git中提交一个空文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处的关键字是为什么.有很多问题的答案是"git不允许这样做",但我想知道为什么不允许这样做.

Key word here is why. There are plenty of questions where the answer is, "git doesn't allow that" but I want to know why it doesn't allow this.

我一直在阅读 git的体系结构,并且其中包含此图像:

I've been reading about the architecture of git and it has this image in it:

此图显示存在 tree节点.从技术上讲,保存tree而不带任何子级看起来很简单.那么为什么git禁止这样做?

This image shows that there are tree nodes. Technically, it looks like it'd be straight forward to save a tree without any children. So why does git forbid this?

本书中有一部分提到了这一点:

There's a part in this book that mentions this:

对于更改后的文件上方的每个目录(加上存储库根目录),将创建一个具有新标识符的新树对象. DAG是从新创建的指向该Blob的根树对象开始创建的(重新使用现有Blob引用,在此提交中文件内容未更改),并引用该新创建的Blob代替文件中先前的Blob对象.先前的树层次结构. (blob代表存储在存储库中的文件.)

我觉得这可能是原因,但这在我想回答的问题上掩盖了细节.

I feel like this might be the reason, but it's kind of glossing over the details with respect to what I want answered.

推荐答案

原因实际上很简单- git索引中的目录仅作为文件路径的一部分存在.

The reason for this is actually simple - directories in the git index only exist as part of the file paths.

如果您有目录dir1dir2,其中dir1包含fileAfileBdir2包含fileC,则git将提交以下内容(即,将以下内容添加到索引文件中):

If you have directories dir1 and dir2, with dir1 containing fileA and fileB and dir2 containing fileC, git will commit the following (i.e. add the following to the index file):

  • dir1/fileA
  • dir1/fileB
  • dir2/fileC
  • dir1/fileA
  • dir1/fileB
  • dir2/fileC

换句话说,dir1dir2都不是真正提交的,它们只是基于其中包含的文件的路径隐式构造的.

In other words, neither dir1 nor dir2 are truly committed, they are simply constructed implicitly based on the paths of files contained inside them.

现在不知道是出于什么更深层次的原因,还是只是使实施更容易,我不知道.

Now whether there are any deeper reasons for this or it simply made the implementation easier, I don't know.

这篇关于为什么我不能在git中提交一个空文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆