通过 Git 远程跟踪分支的给定名称如何找到哪个本地分支跟踪它?

bob*_*eff 2 git version-control

例如,通过给定一个 Git 远程跟踪分支的名称,upstream/develop如果有的话,如何找到哪个本地分支跟踪它?

如果可能,我正在寻找一种不依赖于 shell 脚本并且也适用于 Windows 的解决方案。

Rom*_*eri 6

另一种方法是使用条件格式for-each-ref

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

可以更方便地放入别名中

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
Run Code Online (Sandbox Code Playgroud)

在这个别名中,我假设了一个唯一的遥控器,但是当然,如​​果您希望能够在不同的遥控器上使用它,请稍微调整一下以在参数中包含遥控器名称:

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
Run Code Online (Sandbox Code Playgroud)


Rom*_*eri 5

另一种选择是过滤简单的非常详细的输出git branchgrep

git branch -vv | grep upstream/develop
Run Code Online (Sandbox Code Playgroud)