git rev-parse
git
用法
git rev-parse [--short] <rev>把 HEAD 或 v1.2.0^ 這類名字解析成真正的雜湊,也能告訴你儲存庫在哪;指令稿用 --abbrev-ref HEAD 取分支名,在儲存庫之外它會以錯誤結束。
常用選項
| 選項 | 含義 |
|---|---|
| --short[=<n>] | abbreviate the hash |
| --abbrev-ref HEAD | print the current branch name |
| --show-toplevel | print the root of the working tree |
| --git-dir | print the path of the .git directory |
| --is-inside-work-tree | true or false, for scripts |
| --verify <rev> | fail loudly if the name does not resolve |
範例
git rev-parse --short HEADthe short hash a build should stamp itself with
git rev-parse --abbrev-ref HEADthe branch name, for a CI script
git rev-parse --show-toplevelthe 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