git - 为什么我不能在 git 中提交一个空文件夹?

标签 git

这里的关键词是为什么。有很多问题的答案是“git 不允许这样做”,但我想知道 为什么 它不允许这样做。

我一直在阅读 the architecture of git里面有这张图片:

DAG with tree nodes

此图像显示有 节点。从技术上讲,保存一棵没有任何 child 的 tree 看起来是直截了当的。那么为什么 git 禁止这样做呢?

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

For each directory above the changed file (plus the repository root directory), a new tree object is created with a new identifier. A DAG is created starting from the newly created root tree object pointing to blobs (reusing existing blob references where the files content has not changed in this commit) and referencing the newly created blob in place of that file's previous blob object in the previous tree hierarchy. (A blob represents a file stored in the repository.)

我觉得这可能是原因,但它有点掩盖了我想要回答的细节。

最佳答案

原因其实很简单——git 索引 中的目录仅作为文件路径的一部分存在。

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

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

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

我不知道这是否有更深层次的原因,或者它只是让实现更容易,我不知道。

关于git - 为什么我不能在 git 中提交一个空文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26079618/

相关文章:

database - 如何处理来自不同 Git 功能分支的多个 db alter 脚本?

cocoa - 在 Cocoa 项目上使用版本控制的最佳实践

git - 在尚未 merge 上次提交的同一分支上创建新的 pull 请求

git - 具有多个网站的 git 结构

linux - Git Smart HTTP推送权限失败

Git:如何 rebase 到先前的提交

git - Git 中的提交 : Is it a snapshot/state/image or is it a change/diff/patch/delta?

git - 确定 Octopus merge 期间哪些分支发生冲突

linux - 如何设置 Node 服务器以便我可以使用 git 推送更新?

git - 在 VSTF 构建中查看 git-lfs 文件