特定远程分支名称的 Git 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10075184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git list of branch names of specific remote
提问by ДМИТРИЙ МАЛИКОВ
How can it possible to get all names of some remote origin branches?
如何获得某些远程源分支的所有名称?
I started from --remote --list
options, but got redundant origin/HEAD -> origin/master
message and branches from the another origin.
我从--remote --list
选项开始,但从origin/HEAD -> origin/master
另一个来源获得了冗余消息和分支。
$> git branch --remote --list
origin/HEAD -> origin/master
origin1/develop
origin1/feature/1
origin1/feature/2
origin1/feature/3
origin1/master
origin2/develop
origin2/feature/1
origin2/feature/2
origin2/master
Branches of specific origin could be matched with <pattern>
option, but redundant message is still there. Actually that pattern is not really correct, because some origin's name could be a substring of another origin name, or even some branch.
特定来源的分支可以与<pattern>
选项匹配,但冗余消息仍然存在。实际上,这种模式并不真正正确,因为某个来源的名称可能是另一个来源名称的子字符串,甚至是某个分支。
$> git branch --remote --list origin1*
origin1/HEAD -> origin/master
origin1/develop
origin1/feature/1
origin1/feature/2
origin1/feature/3
origin1/master
What am I looking for is a list of branch names of origin1
, any of them I could use for git checkout
command. Something like that:
我正在寻找的是 的分支名称列表origin1
,其中任何一个我都可以用于git checkout
命令。类似的东西:
develop
feature/1
feature/2
feature/3
master
It's important that it should be done without grep
, sed
, tail
or even ghc -e
wrappers, only with true git
power, because of their unsafeness and variation.
重要的是,它不应该做grep
,sed
,tail
甚至ghc -e
包装,只有真正的git
力量,因为他们unsafeness和变异。
回答by VonC
It's important that it should be done without
grep
,sed
,tail
or evenghc -e
wrappers, only with true git power, because of their unsafeness and variation.
重要的是,它不应该做
grep
,sed
,tail
甚至ghc -e
包装,只有真正的git的力量,因为他们unsafeness和变异。
That is only true for git porcelain commands (see "What does the term porcelain mean in Git?")
这仅适用于 git 瓷器命令(参见“ Git 中瓷器一词是什么意思?”)
Use the plumbing command ls-remote, and then you will be able to filter its output.
使用管道命令ls-remote,然后您将能够过滤其输出。
ls-remote without parameter would still list the remote HEAD:
不带参数的 ls-remote 仍会列出远程 HEAD:
git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote origin
8598d26b4a4bbe416f46087815734d49ba428523 HEAD
8598d26b4a4bbe416f46087815734d49ba428523 refs/heads/master
38325f657380ddef07fa32063c44d7d6c601c012 refs/heads/test_trap
But if you ask only for the heads of said remote:
但是,如果您只询问上述遥控器的负责人:
git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote --heads origin
8598d26b4a4bbe416f46087815734d49ba428523 refs/heads/master
38325f657380ddef07fa32063c44d7d6c601c012 refs/heads/test_trap
Final answer:
最终答案:
git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote --heads origin | sed 's?.*refs/heads/??'
master
test_trap
(Yes, it uses sed
, but the output of a plumbing command is supposed to be stable enough to be parsed)
(是的,它使用sed
,但管道命令的输出应该足够稳定以进行解析)
See also Git 2.23 (Q3 2019) which documents an example:
另请参阅Git 2.23(2019 年第三季度),其中记录了一个示例:
git branch -r -l '<remote>/<pattern>'
git for-each-ref 'refs/remotes/<remote>/<pattern>'
回答by Philip Oakley
An alternate method, after some research into the same problem, is:
在对同一问题进行一些研究之后,另一种方法是:
git for-each-ref --format='%(refname:strip=2)' refs/remotes/<remote_name>
This will give a sorted list of your local refs for the named remote at the point you last fetched.
这将在您上次获取时为指定的远程提供本地引用的排序列表。
You can adjust this for their tags etc.
您可以针对他们的标签等进行调整。
回答by Zitrax
The existing answer both uses something explicitly not wanted in the question (sed) and is a remote command.
现有的答案都使用了问题(sed)中明确不需要的东西,并且是一个远程命令。
I found this that avoids both those issues, uses only local git commands and a pipe:
我发现这避免了这两个问题,只使用本地 git 命令和管道:
git rev-parse --remotes=origin | git name-rev --name-only --stdin
Update: Not really optimal either, but keeping it if someone knows how to improve it. It lists the full remote including the /remotes/originprefix if you have no local branch, but only the local name if you have. In addition it seem to skip some refs if there are several pointing to the same SHA1.
更新:也不是最佳选择,但如果有人知道如何改进它,请保留它。如果您没有本地分支,它会列出完整的远程,包括/remotes/origin前缀,但如果有,则只列出本地名称。此外,如果有多个引用指向同一个 SHA1,它似乎会跳过一些引用。