为什么Git浅克隆可以拥有比<depth>更多的提交?

3

git clone --depth <depth> <url> <repo>; git -C <repo> rev-list --count --all != <depth>

git clone --depth <depth>:创建被截断至 <depth>提交记录历史的克隆版本。意味着使用 --single-branch

例如,url = https://github.com/vhf/free-programming-books.git,depth = 10,但 commit_count = 15。

git version 2.9.0


你使用的 Git 版本是什么?在 2.9.2 版本中,我同时得到了 rev-list(HEAD 和 --all)的 15。 - VonC
@VonC 我也是,但我期望它是10。('HEAD != --all' 是由我的错误引起的,我之后进行了编辑) - xged
1
我怀疑最终的深度是通过确保所有元素至少有10个提交来推导出来的。为此,由于每个提交并不总是涉及所有文件,您有时需要更深入地了解。 - VonC
@VonC 你所说的元素是指文件吗? - xged
1个回答

7

浅层次版本的代码库包括从分支头开始,跟随所有可能的父级而不仅仅是第一个父级,在指定距离内的所有提交。 因此,对于具有合并的非线性历史记录,提交计数将不等于深度。

$ git clone --depth 10 https://github.com/vhf/free-programming-books.git
Cloning into 'free-programming-books'...
remote: Counting objects: 85, done.
remote: Compressing objects: 100% (63/63), done.
remote: Total 85 (delta 31), reused 46 (delta 22), pack-reused 0
Unpacking objects: 100% (85/85), done.
Checking connectivity... done.

$ git -C free-programming-books/ rev-list --count HEAD
15

$ git -C free-programming-books/ log --graph --oneline --decorate
* b9ffc8e (HEAD -> master, origin/master, origin/HEAD) Adding pt_BR C book used by ...
* 824c1d3 Replaced Google Python style guide dead-link with new one (#1987)
* 3c32612 Added Laravel: Code Smart online book (#1986)
* eabce2c Fixed typo: Structure and Interpretation (#1985)
* aab83e5 Added IRPF90 gitbook to Misc section (#1984)
* 6f72509 Added a bash tutorial in free-courses-en.md (#1983)
*   9b95b09 Merge branch 'pr/1980'
|\  
| * 2811cd3 Fix blank lines
| * bbe9bd6 Adds 2 golang podcasts (and fixes missing #ggulp)
| * fdeabc6 (grafted) Fix ordering
*   da317ad Merge branch 'pr/1976'
|\  
| * 20b940a Fix ordering
| * 9a6ee0b (grafted) Add openHPI to list of MOOCs
* 43294d1 Update link Rust by Example #1970 (#1995)
* d758a93 (grafted) Fix a broken link to 'Practical PostgreSQL' (#1994)

这个截断历史中的伪根提交包括d758a939a6ee0bfdeabc6。它们都是头提交的第九代祖先。请注意,当<url>不以file://开头时,这可能发生在本地克隆中。此时,Git会输出相应的警告信息。
warning: --depth is ignored in local clones; use file:// instead.

对于本地克隆,git会简单地创建到源代码库中对象的硬链接(除非指定--no-hardlinks选项,但后者仍然不会使本地克隆体现--depth选项)。通过硬链接进行克隆可节省磁盘空间,并且与复制所有对象相比速度非常快。由于--depth选项的目的是减少数据传输,因此对于本地克隆来说这并没有太多意义,因此该选项被忽略。

我没有收到警告。 - xged
提交计数随深度变化而变化。因此,深度不被忽略。 - xged

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