通过Git远程跟踪分支的给定名称如何找到哪个本地分支跟踪它?
例如,通过给定一个 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