git rev-parse 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15798862/
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 15:55:03  来源:igfitidea点击:

What does git rev-parse do?

gitgit-rev-parse

提问by talles

What does git rev-parsedo?

有什么作用git rev-parse

I have read the man page but it raised more questions than answers. Things like:

我已经阅读了手册页,但它提出的问题多于答案。像:

Pick out and massageparameters

挑选和按摩参数

Massage? What does that mean?

按摩?这意味着什么?

I'm using as a resolver (to SHA1) of revision specifiers, like

我正在用作修订说明符的解析器(到 SHA1),例如

git rev-parse HEAD^

or

或者

git rev-parse origin/master

Is this the command's purpose? If not, is even correct to use it to achieve this?

这是命令的目的吗?如果没有,使用它来实现这一目标是否正确?

采纳答案by Tuxdude

git rev-parseis an ancillary plumbingcommand primarily used for manipulation.

git rev-parseplumbing主要用于操作的辅助命令。

One common usage of git rev-parseis to print the SHA1 hashes given a revision specifier. In addition, it has various options to format this output such as --shortfor printing a shorter unique SHA1.

的一种常见用法git rev-parse是打印给定修订说明符的 SHA1 哈希值。此外,它还具有多种选项来格式化此输出,例如--short打印更短的唯一 SHA1。

There are other use cases as well (in scripts and other tools built on top of git) that I've used for:

还有其他用例(在基于 git 构建的脚本和其他工具中)我已经用于:

  • --verifyto verify that the specified object is a valid git object.
  • --git-dirfor displaying the abs/relative path of the the .gitdirectory.
  • Checking if you're currently within a repository using --is-inside-git-diror within a work-tree using --is-inside-work-tree
  • Checking if the repo is a bare using --is-bare-repository
  • Printing SHA1 hashes of branches (--branches), tags (--tags) and the refs can also be filtered based on the remote (using --remote)
  • --parse-optto normalize arguments in a script (kind of similar to getopt) and print an output string that can be used with eval
  • --verify验证指定的对象是否是有效的 git 对象。
  • --git-dir用于显示目录的绝对/相对路径.git
  • 使用以下命令检查您当前是否在存储库中--is-inside-git-dir或在工作树中--is-inside-work-tree
  • 检查回购是否是裸使用 --is-bare-repository
  • 打印分支 ( --branches)、标签 ( --tags) 和 refs 的SHA1 哈希值也可以基于远程过滤(使用--remote
  • --parse-opt规范化脚本中的参数(类似于getopt)并打印可用于的输出字符串eval

Massagejust implies that it is possible to convert the info from one form into another i.e. a transformation command. These are some quick examples I can think of:

Massage只是暗示可以将信息从一种形式转换为另一种形式,即转换命令。这些是我能想到的一些快速示例:

  • a branch or tag name into the commit's SHA1 it is pointing to so that it can be passed to a plumbing command which only accepts SHA1 values for the commit.
  • a revision range A..Bfor git logor git diffinto the equivalent arguments for the underlying plumbing command as B ^A
  • 将分支或标记名称添加到它所指向的提交的 SHA1 中,以便可以将其传递给只接受提交的 SHA1 值的管道命令。
  • 版本范围A..Bgit loggit diff为用于底层的管道命令作为等效参数B ^A

回答by scanny

Just to elaborate on the etymology of the command name rev-parse, Git consistently uses the term revin plumbing commands as short for "revision" and generally meaning the 40-character SHA1 hash for a commit. The command rev-listfor example prints a list of 40-char commit hashes for a branch or whatever.

只是为了详细说明命令名称的词源rev-parse,Gitrev在管道命令中始终使用该术语作为“修订”的缩写,通常表示提交的 40 个字符的 SHA1 哈希。rev-list例如,该命令会打印一个分支或其他任何内容的 40-char 提交哈希列表。

In this case the name might be expanded to parse-a-commitish-to-a-full-SHA1-hash. While the command has the several ancillary functions mentioned in Tuxdude's answer, its namesake appears to be the use case of transforming a user-friendly reference like a branch name or abbreviated hash into the unambiguous 40-character SHA1 hash most useful for many programming/plumbing purposes.

在这种情况下,名称可能会扩展为parse-a-commitish-to-a-full-SHA1-hash. 虽然该命令具有 Tuxdude 的回答中提到的几个辅助功能,但它的同名似乎是将用户友好的引用(如分支名称或缩写哈希)转换为对许多编程/管道最有用的明确 40 个字符的 SHA1 哈希的用例目的。

I know I was thinking it was "reverse-parse" something for quite a while before I figured it out and had the same trouble making sense of the terms "massaging" and "manipulation" :)

我知道在我弄清楚之前我一直认为它是“反向解析”的东西,并且在理解“按摩”和“操纵”这两个术语时遇到了同样的麻烦:)

Anyway, I find this "parse-to-a-revision" notion a satisfying way to think of it, and a reliable concept for bringing this command to mind when I need that sort of thing. Frequently in scripting Git you take a user-friendly commit reference as user input and generally want to get it resolved to a validated and unambiguous working reference as soon after receiving it as possible. Otherwise input translation and validation tends to proliferate through the script.

无论如何,我发现这种“解析到修订”的概念是一种令人满意的思考方式,并且是一个可靠的概念,可以在我需要那种东西时想起这个命令。通常在编写 Git 脚本时,您将用户友好的提交参考作为用户输入,并且通常希望在收到后尽快将其解析为经过验证且明确的工作参考。否则输入翻译和验证往往会通过脚本激增。

回答by mitra

git rev-parseAlso works for getting the current branch name using the --abbrev-refflag like:

git rev-parse也适用于使用--abbrev-ref标志获取当前分支名称,例如:

git rev-parse --abbrev-ref HEAD