首页·终端命令

git rev-parse

git

用法

git rev-parse [--short] <rev>

把 HEAD 或 v1.2.0^ 这类名字解析成真正的哈希,也能告诉你仓库在哪;脚本用 --abbrev-ref HEAD 取分支名,在仓库之外它会以错误退出。

常用选项

选项含义
--short[=<n>]abbreviate the hash
--abbrev-ref HEADprint the current branch name
--show-toplevelprint the root of the working tree
--git-dirprint the path of the .git directory
--is-inside-work-treetrue or false, for scripts
--verify <rev>fail loudly if the name does not resolve

示例

git rev-parse --short HEAD

the short hash a build should stamp itself with

git rev-parse --abbrev-ref HEAD

the branch name, for a CI script

git rev-parse --show-toplevel

the repository root, wherever you are inside it

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git rev-parse 是做什么的?

把 HEAD 或 v1.2.0^ 这类名字解析成真正的哈希,也能告诉你仓库在哪;脚本用 --abbrev-ref HEAD 取分支名,在仓库之外它会以错误退出。

Q. 怎么写?

git rev-parse [--short] <rev> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-rev-parse