有幾種可能的方法來獲取當前分支的名稱:
current-branch1 = name-rev --name-only HEAD
current-branch2 = branch --show-current
#1 在標簽存在的情況下無法正常作業......
#2 確實作業得更好……除非 HEAD 指向分離狀態。
是否有更好的別名來知道提交是在分離狀態下完成的哪個分支?換句話說,一個在所有情況下都能更好地作業的別名?
更新 -在檢查提交時,我想獲取提交完成的分支的名稱...
$ g co 2767c5f9
Note: switching to '2767c5f9'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in
this state without impacting any branches by switching back to a branch.
[...]
HEAD is now at 2767c5f9 Update ChangeLog
$ git for-each-ref --format="%(refname:short)" --points-at HEAD refs/heads
最后一個命令什么都不回傳...
uj5u.com熱心網友回復:
就在這里 :
# any ref (branch or tag or remote branch or ...)
git for-each-ref --format="%(refname:short)" --points-at HEAD
# branches only :
git for-each-ref --format="%(refname:short)" --points-at HEAD refs/heads
警告:如果有多個 refs/branch 指向當前提交,則上述命令將全部列出。
[編輯] git name-rev也有一個--refs選項,你只需要知道分支是以 開頭的參考refs/heads:
git name-rev --name-only --refs="refs/heads/*" HEAD
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/387338.html
