可以使用下面命令查看本地分支在遠(yuǎn)端服務(wù)器的分支名:
1
|
$ git rev-parse --abbrev-ref local_branch_name@{upstream} |
把 local_branch_name 換成要查詢的本地分支名,例如 master 等。下面通過例子來說明這個命令各個參數(shù)的含義。
先創(chuàng)建一個新的本地分支,名為 new_local_branch,關(guān)連到遠(yuǎn)端服務(wù)器的 Remote_Branch_U 分支:
1
2
3
|
$ git checkout -b new_local_branch aosp /Remote_Branch_U Branch new_local_branch set up to track remote branch Remote_Branch_U from aosp. Switched to a new branch 'new_local_branch' |
查看本地分支 new_local_branch 在遠(yuǎn)端服務(wù)器的分支名:
1
2
|
$ git rev-parse --abbrev-ref new_local_branch@{upstream} aosp /Remote_Branch_U |
如果所給的本地分支名沒有關(guān)連到遠(yuǎn)端服務(wù)器分支,會打印報錯信息:
1
2
|
$ git rev-parse --abbrev-ref great@{upstream} fatal: No upstream configured for branch 'great' |
注意:@{upstream} 這一整串本身是命令的一部分,直接輸入即可,不是要把 upstream 或者
{upstream} 替換成遠(yuǎn)端服務(wù)器倉庫名。查看 man git-rev-parse 有如下說明:
<branchname>@{upstream}, e.g. master@{upstream}, @{u}
The suffix @{upstream} to a branchname (short form <branchname>@{u}) refers to the branch that the branch specified by branchname is set to build on top of. A missing branchname defaults to the current one.
即,@{upstream} 可以縮寫為 @{u}。如果不提供分支名,默認(rèn)用當(dāng)前本地分支名。
另外,如果不加 --abbrev-ref 選項(xiàng),會打印分支head的hash值,而不是打印分支名。
1
2
3
4
5
6
|
$ git rev-parse --abbrev-ref new_local_branch@{u} aosp /Remote_Branch_U $ git rev-parse --abbrev-ref @{u} aosp /Remote_Branch_U $ git rev-parse @{u} 66355f171f5ba7dbc66465e761b97afe2395b06e |
這個命令可在shell腳本中自動獲取到遠(yuǎn)端服務(wù)器分支名,而不是只能用默認(rèn)值、或者要手動輸入分支名,方便自動化處理。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000020811946