簡體   English   中英

通過一個Git遠程跟蹤分支的名字如何找到本地分支跟蹤它?

[英]By given name of a Git remote-tracking branch how to find which local branch tracks it?

通過 Git 遠程跟蹤分支的給定名稱,例如, upstream/develop如何找到哪個本地分支跟蹤它(如果有)?

如果可能的話,我正在尋找一種不依賴於 shell 腳本並且也適用於 Windows 的解決方案。

另一種方法是使用帶有for-each-ref條件格式

git for-each-ref --format="%(if:equals=upstream/develop)%(upstream:short)%(then)%(refname:short)%(end)" refs/heads | sort -u

可以更方便地放入別名中

git config --global alias.who-tracks '!f() { git for-each-ref --format="%(if:equals=upstream/$1)%(upstream:short)%(then)%(refname:short)%(end)" refs/heads | sort -u; }; f'

# then when you need it :
git who-tracks develop
git who-tracks another/branch

在這個別名中,我假設了一個唯一的遙控器,但當然,如果您希望能夠在不同的遙控器上使用它,請稍作調整以在參數中包含遙控器名稱:

git config --global alias.who-tracks '!f() { git for-each-ref --format="%(if:equals=$1)%(upstream:short)%(then)%(refname:short)%(end)" refs/heads | sort -u; }; f'

# then when you need it :
git who-tracks upstream/develop
git who-tracks origin/another/branch

另一種選擇是使用 grep 過濾簡單git branch非常冗長grep

git branch -vv | grep upstream/develop

基於這個答案(分支枚舉)和這個答案(檢索上游分支),您可以遍歷本地分支並檢查它們中的任何一個是否具有所需的跟蹤遠程分支:

git for-each-ref --shell \
  --format='test %(upstream:short) = "upstream/develop" && echo %(refname:short)' \
  refs/heads/ | sh

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM