简体   繁体   English

通过一个Git远程跟踪分支的名字如何找到本地分支跟踪它?

[英]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.

相关问题 如何创建git Remote-Tracking分支 - How to create git Remote-Tracking Branch 当使用git删除相应的本地分支时,如何自动删除远程跟踪分支 - How to automatically remove remote-tracking branch when the corresponding local branch is removed using git Git:给定远程跟踪分支-如何找到它正在跟踪的远程/分支 - Git: Given remote tracking branch - How to find which remote/branch it is tracking 如何判断哪个远程svn分支是本地git分支跟踪? - How to tell which remote svn branch is a local git branch tracking? Git:如何获取单个远程分支并自动创建远程跟踪分支? - Git: How to fetch a single remote branch and create a remote-tracking branch of it automatically? Git - 推送到远程存储库中的远程跟踪分支 - Git - push to a remote-tracking branch in the remote repository 它们的名称是否相同:本地跟踪分支,相应的远程跟踪分支以及正在跟踪的相应远程分支? - Are their names the same: a local-tracking branch, the corresponding remote-tracking branch, and the corresponding remote branch being tracked? 删除远程分支时如何删除本地远程跟踪分支? - How do I delete local remote-tracking branches when remote branch is deleted? 找出本地分支正在跟踪哪个远程 - Find out which remote a local branch is tracking 如何在Git中不预先合并远程跟踪提交的情况下创建新分支? - How to create a new branch without pre-merge remote-tracking commits in Git?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM