[英]By given name of a Git remote-tracking branch how to find which local branch tracks it?
By a given name of a Git remote-tracking branch, for example, upstream/develop
how to find which local branch tracks it if any?通过 Git 远程跟踪分支的给定名称,例如, upstream/develop
如何找到哪个本地分支跟踪它(如果有)?
If possible I'm looking for a solution that not relies on shell scripting and also works on Windows.如果可能的话,我正在寻找一种不依赖于 shell 脚本并且也适用于 Windows 的解决方案。
An alternative is to use a conditional format with for-each-ref
另一种方法是使用带有for-each-ref
的条件格式
git for-each-ref --format="%(if:equals=upstream/develop)%(upstream:short)%(then)%(refname:short)%(end)" refs/heads | sort -u
which could be more conveniently put into an alias like可以更方便地放入别名中
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
In this alias, I assumed a unique remote, but of course if you want to be able to use it on different remotes, tweak a little bit to include the remote name in the parameter:在这个别名中,我假设了一个唯一的遥控器,但当然,如果您希望能够在不同的遥控器上使用它,请稍作调整以在参数中包含遥控器名称:
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
Another alternative is to filter the very verbose output of the simple git branch
with grep
另一种选择是使用 grep 过滤简单git branch
的非常冗长的grep
git branch -vv | grep upstream/develop
Based on this answer (branch enumeration) and this answer (retrieving upstream branch), you can iterate through local branches and check if any of them has the desired tracking remote branch:基于这个答案(分支枚举)和这个答案(检索上游分支),您可以遍历本地分支并检查它们中的任何一个是否具有所需的跟踪远程分支:
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.