使用 git branch 查看分支,會打印倉庫下的所有分支名,通過 '*' 星號來標識當前分支。
如果想打印且打印當前本地分支名,可以用 git symbolic-ref --short HEAD
命令。
$ git branch
* curent_branch_xxx
enable_func
$ git symbolic-ref --short HEAD
curent_branch_xxx
也可以使用 git rev-parse --abbrev-ref HEAD
命令來打印且只打印當前分支名。
$ git rev-parse --abbrev-ref HEAD
curent_branch_xxx
這兩個命令可用在shell腳本中獲取當前分支名,做一些自動化處理。
git symbolic-ref --short HEAD
使用 man git-symbolic-ref 查看該命令的幫助信息,說明如下:
git symbolic-ref: Read, modify and delete symbolic refs
git symbolic-ref [-m <reason>] <name> <ref>
git symbolic-ref [-q] [--short] <name>
Given one argument, reads which branch head the given symbolic ref refers to and outputs its path, relative to the .git/ directory. Typically you would give HEAD as the <name> argument to see which branch your working tree is on.--short
When showing the value of <name> as a symbolic ref, try to shorten the value, e.g. from refs/heads/master to master.
即,git symbolic-ref 命令可以查看 symbolic ref 的信息。而 HEAD 就是一個 symbolic ref 的名稱,可用於查看當前工作分支。
使用 man git 可以查看到 git refs、HEAD 的一些說明:
Named pointers called refs mark interesting points in history. A ref may contain the SHA-1 name of an object or the name of another ref. Refs with names beginning ref/head/ contain the SHA-1 name of the most recent commit (or "head") of a branch under development. SHA-1 names of tags of interest are stored under ref/tags/. A special ref named HEAD contains the name of the currently checked-out branch.
查看 github 網站上的開發者文檔 (https://developer.github.com/...),有如下說明:
A Git reference (git ref) is just a file that contains a Git commit SHA-1 hash. When referring to a Git commit, you can use the Git reference, which is an easy-to-remember name, rather than the hash. The Git reference can be rewritten to point to a new commit. A branch is just a Git reference that stores the new Git commit hash.
即,git ref 保存了git commit hash值,git ref 本身有一個名稱,被用作分支名。一個分支其實就是一個 git ref。不同分支的差異是分支head指向的 git commit hash 不同。
HEAD 是一個指向當前工作分支的 git ref,切換工作分支,會改變 HEAD 的指向。這個文件在git倉庫中的路徑是 .git/HEAD
,可以用cat命令查看它的內容,下面的 HEAD 指向 master分支:
$ cat .git/HEAD
ref: refs/heads/master
各個 git ref 存在 .git/refs/
目錄下,本地分支head存在 .git/refs/heads/
目錄下:
$ ls .git/refs/heads
great master
可以看到,git branch 的分支名跟 .git/refs/heads/
目錄下的文件名相同:
$ git branch
great
* master
結合這幾個說明,對 git symbolic-ref --short HEAD
命令分解說明如下:
-
git symbolic-ref
命令可以解析獲取 git ref 的信息 -
--short
表示獲取 symbolic ref 的名稱 -
HEAD
是指向當前工作分支的 git ref,解析HEAD文件信息,就能獲取當前分支名
git rev-parse --abbrev-ref HEAD
git rev-parse --abbrev-ref HEAD
命令也能獲取當前分支名。查看 man git-rev-parse 的說明如下:
git rev-parse: Pick out and massage parameters
git rev-parse [ --option ] <args>...
--abbrev-ref[=(strict|loose)]
A non-ambiguous short name of the objects name.
在man手冊裏面沒有具體說明這個命令的表現是什麼。在git的在線參考鏈接上找到一些描述: https://git-scm.com/book/en/v...
If you want to see which specific SHA-1 a branch points to, or if you want to see what any of these examples boils down to in terms of SHA-1s, you can use a Git plumbing tool called rev-parse; basically, rev-parse exists for lower-level operations and isn’t designed to be used in day-to-day operations.
大致理解,git rev-parse 是一種git管道(plumbing)工具,可用於處理SHA-1 hash值。
我們在上面看到,git rev-parse --abbrev-ref HEAD
打印當前分支名,--abbrev-ref 表示輸出所給對象不會混淆的短名,類似於 git symbolic-ref 的 --short 選項的輸出結果。
不加 --abbrev-ref 時,會打印出HEAD對應的hash值:
$ git rev-parse HEAD
8ebf0117f9545187d3368adc1ce629608214984a
這兩個命令都可以打印當前分支名,如果當前處於未命名分支下面時,它們的行爲會有一些差異。下面用 git branch 命令查看,可以看到當前處在分離的HEAD狀態下,當前分支沒有命名:
$ git branch
* (detached from 65c6917)
great
master
在這種情況下,HEAD不是符號引用,git symbolic-ref 會以錯誤退出:
$ git symbolic-ref --short HEAD
fatal: ref HEAD is not a symbolic ref
而 git rev-parse –abbrev-ref 將HEAD解析爲自身:
$ git rev-parse --abbrev-ref HEAD
HEAD