git - tree-ish 在 Git 中是什么意思?

标签 git git-archive

我很困惑如何使用 git archive .

我有一个 git 存储库,其中文件夹 FooBarBaz 位于顶层。我需要以类似 SVN 的方式导出文件夹 Foo 以进行快速测试部署。

我了解到我可以使用 git-archiveSVN-ish export sort of way .

但事情是这样的,以下工作正常:

git archive master | tar -x -C ~/destination

它会在目标 文件夹中生成FooBarBaz 文件夹。

但是,以下将出错 fatal not a valid object name :

git archive master/foo | tar -x -C ~/destination

文档

寻找 git archive 的概要程序我看到它可以使用 <tree-ish> [path]作为参数(相关部分的概要):

git archive <tree-ish> [path...]

如果 master/foo 不是 tree-ish ,那又是什么?

最佳答案

简短答案(TL;DR)

“树状”是指最终指向(子)目录的任何标识符(如 the Git revisions documentation 中指定的) 树(Git 将目录称为“树”和“树对象”)。

在原发帖者的例子中,foo 是他想要的目录 指定。在 Git 中指定(子)目录的正确方法是使用这个 “树状”语法(来自 the Git revisions documentation 的第 15 项):

<rev>:<path>, e.g. HEAD:README, :README, master:./README

A suffix : followed by a path names the blob or tree at the given path in the tree-ish object named by the part before the colon.

因此,换句话说, master:foo 是正确的语法,而不是 master/foo

其他“树式”(加上提交式)

这是提交式和树式标识符的完整列表(来自 the Git revisions documentationthanks to LopSae for pointing it out ):

----------------------------------------------------------------------
|    Commit-ish/Tree-ish    |                Examples
----------------------------------------------------------------------
|  1. <sha1>                | dae86e1950b1277e545cee180551750029cfe735
|  2. <describeOutput>      | v1.7.4.2-679-g3bee7fb
|  3. <refname>             | master, heads/master, refs/heads/master
|  4. <refname>@{<date>}    | master@{yesterday}, HEAD@{5 minutes ago}
|  5. <refname>@{<n>}       | master@{1}
|  6. @{<n>}                | @{1}
|  7. @{-<n>}               | @{-1}
|  8. <refname>@{upstream}  | master@{upstream}, @{u}
|  9. <rev>^                | HEAD^, v1.5.1^0
| 10. <rev>~<n>             | master~3
| 11. <rev>^{<type>}        | v0.99.8^{commit}
| 12. <rev>^{}              | v0.99.8^{}
| 13. <rev>^{/<text>}       | HEAD^{/fix nasty bug}
| 14. :/<text>              | :/fix nasty bug
----------------------------------------------------------------------
|       Tree-ish only       |                Examples
----------------------------------------------------------------------
| 15. <rev>:<path>          | HEAD:README, :README, master:./README
----------------------------------------------------------------------
|         Tree-ish?         |                Examples
----------------------------------------------------------------------
| 16. :<n>:<path>           | :0:README, :README
----------------------------------------------------------------------

标识符 #1-14 都是“commit-ish”,因为它们都会导致提交,但是 因为提交也指向目录树,所以它们最终都会导致 (子)目录树对象,因此也可以用作“树状”。

#15 在引用(子)目录时也可以用作树状结构,但它 也可用于识别特定文件。当它提到文件时,我不是 确定它是否仍然被认为是“树状”,或者如果行为更像“blob-ish”(Git 将文件称为“blob”)。

长答案

在最低级别,Git 使用四种基本的方式跟踪源代码 对象:

  1. 带注释的标签,指向提交。
  2. 提交,指向项目的根目录树。
  3. 树,即目录和子目录。
  4. Blob,也就是文件。

这些对象中的每一个都有自己的 sha1 哈希 ID,因为 Linus Torvalds 设计了 Git 类似于 content- addressable 文件系统,即可以检索文件 基于它们的内容(sha1 ID 是从文件内容生成的)。临 Git 本书给出了 this example diagram :

Figure 9-3 from Pro Git book

许多 Git 命令可以接受提交和(子)目录的特殊标识符 树木:

  • “提交式”是最终导致提交对象的标识符。例如,

    tag -> commit

  • “树状”是最终导致树(即目录)对象的标识符。

    tag -> commit -> project-root-directory

因为提交对象总是指向一个目录树对象(根 你项目的目录),任何“commit-ish”的标识符都是 定义,也是“树状”。换言之,任何指向 commit 对象也可用于引导(子)目录树对象

但是由于目录树对象从不指向 Git 版本控制中的提交 系统,并非指向(子)目录树的每个标识符也可以 用于指向提交。换句话说,一组“commit-ish”标识符 是“树状”标识符集的严格子集。

the documentation ( thanks to Trebor for helping me find it ) 中所述:

<tree>

Indicates a tree object name.

<commit>

Indicates a commit object name.

<tree-ish>

Indicates a tree, commit or tag object name. A command that takes a <tree-ish> argument ultimately wants to operate on a <tree> object but automatically dereferences <commit> and <tag> objects that point at a <tree>.

<commit-ish>

Indicates a commit or tag object name. A command that takes a <commit-ish> argument ultimately wants to operate on a <commit> object but automatically dereferences <tag> objects that point at a <commit>.

不能用作 commit-ish 的树形标识符集是

  1. <rev>:<path> ,它直接到目录树,不提交 对象。例如,HEAD:subdirectory

  2. 目录树 对象的 Sha1 标识符。

关于git - tree-ish 在 Git 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4044368/

相关文章:

Git 使用远程 Master 更新本地分支

git - 将所有提交导出到 ZIP 文件或目录中

git - 多次提交后添加 gitignore

git - 我如何让 github 将 PR 与特定的电子邮件/用户信息 merge ?

java - 我应该 checkin 这些 gradle 文件吗?

git - 如何从 git 存档中排除文件?

git - 查找未注明日期的提交日期?

git 存档 : how to ignore pathspec during archive?

git - 如何附加分支名称并提交到 git 存档输出文件?