Git:git-rev-parse 命令学习

概要

Git:git-rev-parse 命令学习

博客

原帖收藏于IT老兵博客

前言

在研究Jenkins的时候,遇到了git rev-parse这个命令,这里学习一下这个命令。

正文

git-rev-parse - Pick out and massage parameters

这是这个命令的概要解释,理解起来有一点障碍,挑选出来并且“按摩”参数,这个有点意思啊,对参数进行一下按摩。(这里这个massage是不是应该按照调整来理解呢?)

Many Git porcelainish commands take mixture of flags (i.e. parameters that begin with a dash -) and parameters meant for the underlying git rev-list command they use internally and flags and parameters for the other commands they use downstream of git rev-list. This command is used to distinguish between them.

许多Git porcelainish命令(这个地方不知道怎么理解)会混合使用标志(即以短划线开头的参数-)以及参数,这些参数用于git rev-list它们在内部使用的基础命令,以及标志和参数用于其(git rev-list)下游使用的其他命令。这个命令用于区分它们。

看到这里,感觉这个命令好像只是为了鉴别这些参数是否用于git rev-list的,底下参考的stackoverflow的帖子说,这是一个辅助的探测(管道)工具。

git rev-parse is an ancillary plumbing command primarily used for manipulation.
–verify to verify that the specified object is a valid git object.

例如一些常见场景:
显示指定提交号的SHA1

1
2
$ git rev-parse 5a382c9
5a382c95394410bb716d92ee7418c8e3a17eb8c3

验证指定的对象是一个有效的git对象

1
2
$ git rev-parse --verify HEAD
a1498cc2435b5df53653279395dac42e14e1d393

–verify
Verify that exactly one parameter is provided, and that it can be turned into a raw 20-byte SHA-1 that can be used to access the object database. If so, emit it to the standard output; otherwise, error out.

这里会去通过HEAD去获取a1498cc2435b5df53653279395dac42e14e1d393,然后通过这个去访问对象的数据库。

显示git默认目录

1
2
3
$ git rev-parse --git-dir
.git

以下还有几项,不具体翻译了。

  • Checking if you’re currently within a repository using –is-inside-git-dir or 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-opt to normalize arguments in a script (kind of similar to getopt) and print an output string that can be used with eval

Massage just 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,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.

把一个分支名或者tag名转换成了一个提交的SHA1,要是从这个意义来理解,这个plumbing还真是管道的意思。

  • a revision range A..B for git log or git diff into the equivalent arguments for the underlying plumbing command as B ^A

总结

这个命令有点绕,第一天完全没看懂,第二天才初步搞明白一点。

参考

https://git-scm.com/docs/git-rev-parse 官网
https://cloud.tencent.com/developer/section/1138781 这里有点意思,是对上面官网的直接翻译
https://stackoverflow.com/questions/15798862/what-does-git-rev-parse-do 这里讲了一些常见的用法