首頁·終端機命令

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