Git 中的 commit-ish 和 tree-ish 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23303549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:01:59  来源:igfitidea点击:

What are commit-ish and tree-ish in Git?

git

提问by

The Question

问题

What are specific examples of commit-ish and tree-ish in Git?

Git 中 commit-ish 和 tree-ish 的具体例子是什么?

The Stack Overflow question "What does tree-ish mean in git?"deals with tree-ish specifically, but I want to understand more about both.

Stack Overflow 问题“git 中的 tree-ish 是什么意思?” 专门处理 tree-ish ,但我想更多地了解两者

Background

背景

Usages in Documentation

文档中的用法

The Git documentationmakes several references to "commit-ish" and "tree-ish". For example, if you're examining the Git source code:

Git 文档多次提到“commit-ish”和“tree-ish”。例如,如果您正在检查Git 源代码

$ git grep --files-with-matches --extended-regexp "commit(-)*ish"
config.txt
git-describe.txt
git-fast-import.txt
git-name-rev.txt
git-push.txt
git-rebase.txt
git-rev-parse.txt
git.txt
gitcli.txt
glossary-content.txt
howto/revert-branch-rebase.txt
revisions.txt

and

$ git grep --files-with-matches --extended-regexp "tree(-)*ish" | \
$ grep --invert-match RelNotes
diff-format.txt
diff-generate-patch.txt
git-archive.txt
git-cat-file.txt
git-checkout.txt
git-diff-index.txt
git-diff-tree.txt
git-ls-files.txt
git-ls-tree.txt
git-merge-tree.txt
git-read-tree.txt
git-reset.txt
git-svn.txt
git.txt
gitcli.txt
gittutorial-2.txt
glossary-content.txt
revisions.txt

Definitions

定义

The Git documentation defines what "commit-ish" and "tree-ish" are:

Git 文档定义了“commit-ish”和“tree-ish”是什么

<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>.

<tree>

表示树对象名称。

<commit>

表示提交对象名称。

<tree-ish>

表示树、提交或标记对象名称。一个接受<tree-ish>参数的命令最终想要对一个<tree>对象进行操作,但会自动取消引用<commit><tag>指向 a 的对象<tree>

<commit-ish>

表示提交或标记对象名称。带有<commit-ish>参数的命令最终想要对一个<commit>对象进行操作,但会自动取消引用<tag>指向 a 的对象<commit>

The Documentation isn't Clear Enough

文档不够清楚

Even though the documentation above defines what "commit-ish" and "tree-ish" are, I still find it to be too vague and unclear.

即使上面的文档定义了“commit-ish”和“tree-ish”是什么,我仍然觉得它太模糊和不清楚。

What are specific examples of "commit-ish" and "tree-ish", and how are they different from each other?

“commit-ish”和“tree-ish”的具体例子是什么,它们之间有什么不同?

回答by

The Short Answer (TL;DR)

简答 (TL;DR)

Here's a complete list of commit-ish and tree-ish identifiers (from the Git revisions documentation):

这是一个完整的 commit-ish 和 tree-ish 标识符列表(来自Git 修订文档):

----------------------------------------------------------------------
|    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.txt, master:sub-directory/
----------------------------------------------------------------------
|         Tree-ish?         |                Examples
----------------------------------------------------------------------
| 16. :<n>:<path>           | :0:README, :README
----------------------------------------------------------------------

Identifiers #1-14 are all "commit-ish", because they all lead to commits, but because commits also point to directory trees, they all ultimately lead to (sub)directory tree objects, and can therefore also be used as "tree-ish".

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

#15 can also be used as tree-ish when it refers to a (sub)directory, but it can also be used to identify specific files. When it refers to files, I'm not sure if it's still considered "tree-ish", or if acts more like "blob-ish" (Git refers to files as "blobs").

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

The Long Answer

长答案

Commits and Directory Trees in Git

Git 中的提交和目录树

At its lowest levels, Git keeps track of source code using four fundamental objects:

在最底层,Git 使用四个基本对象来跟踪源代码:

  1. Annotated tags, which point to commits.
  2. Commits, which point to the root directory tree of your project.
  3. Trees, which are directories and subdirectories.
  4. Blobs, which are files.
  1. 带注释的标签,指向提交。
  2. 提交,指向项目的根目录树。
  3. 树,即目录和子目录。
  4. Blob,它们是文件。

Each of these objects has its own sha1 hash ID, since Linus Torvalds designed Git like an content- addressablefilesystem, i.e. files can be retrieved based on their content (sha1 IDs are generated from file content). The Pro Git book gives this example diagram:

这些对象中的每一个都有自己的 sha1 哈希 ID,因为 Linus Torvalds 将 Git 设计为一个内容可寻址的文件系统,即可以根据其内容检索文件(sha1 ID 是从文件内容生成的)。Pro Git book 给出了这个示例图

Figure 9-3 from Pro Git book

图 9-3 来自 Pro Git 书籍

Commit-ish vs Tree-ish

Commit-ish 与 Tree-ish

Many Git commands can accept special identifiers for commits and (sub)directory trees:

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

  • "Commit-ish" are identifiers that ultimately lead to a commit object. For example,

    tag -> commit

  • "Tree-ish" are identifiers that ultimately lead to tree (i.e. directory) objects.

    tag -> commit -> project-root-directory

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

    tag -> commit

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

    tag -> commit -> project-root-directory

Because commit objects always point to a directory tree object (the root directory of your project), any identifier that is "commit-ish" is, by definition, also "tree-ish". In other words, any identifier that leads to a commit object can also be used to lead to a (sub)directory tree object.

由于提交对象始终指向目录树对象(项目的根目录),因此任何“commit-ish”标识符根据定义也是“tree-ish”。换句话说,任何指向提交对象的标识符也可以用来指向(子)目录树对象

But since directory tree objects never point to commits in Git's versioning system, not every identifier that points to a (sub)directory tree can also be used to point to a commit. In other words, the set of "commit-ish" identifiers is a strict subset of the set of "tree-ish" identifiers.

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

The set of tree-ish identifiers that cannot be used as commit-ishare

不能用作 commit-ish的一组 tree-ish 标识符是

  1. <rev>:<path>, which leads directlyto directory trees, not commit objects. For example, HEAD:subdirectory.

  2. Sha1 identifiers of directory treeobjects.

  1. <rev>:<path>,这直接导致目录树,而不是提交对象。例如,HEAD:subdirectory

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

回答by MikeW

Note for non-native English [sic!] speakers: "-ish" is a suffix that can be applied to an adjective in order to indicate "having qualities like" or "slightly" - see http://chambers.co.uk/search/?query=ish&title=21st

非英语母语 [原文如此!] 发言者的注意事项:“-ish”是一个后缀,可用于形容词以表示“具有类似”或“轻微”的品质 - 请参阅http://chambers.co.uk /search/?query=ish&title=21st

Hence "tree-ish" - like a "tree" .... "commit-ish" - like a "commit"

因此,“tree-ish”——就像一棵“树”......“commit-ish”——就像一个“提交”

eg "Mars appears like a reddish star" ("d" is doubled !); "the food on the plate was not hot, but warmish"

例如“火星看起来像一颗红色的星星”(“d”加倍!);“盘子里的食物不是热的,而是暖和的”

I believe that this helps explain "what are ..." better, in that it explains the language usage.

我相信这有助于更好地解释“什么是……”,因为它解释了语言用法。