Git特定远程分支名称列表

ДМИ*_*КОВ 28 git git-branch

怎么可能得到一些远程起源分支的所有名称?

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

特定原点的分支可以与<pattern>选项匹配,但冗余消息仍然存在.实际上,这种模式并不是真正正确的,因为某些原点的名称可能是另一个原始名称的子串,甚至是某个分支.

$> git branch --remote --list origin1*
  origin1/HEAD -> origin/master
  origin1/develop
  origin1/feature/1
  origin1/feature/2
  origin1/feature/3
  origin1/master
Run Code Online (Sandbox Code Playgroud)

我在寻找的是一个分支名称列表origin1,其中任何一个都可以用于git checkout命令.像这样的东西:

develop
feature/1
feature/2
feature/3
master
Run Code Online (Sandbox Code Playgroud)

重要的是,它不应该做grep,sed,tail甚至ghc -e包装,只有真正的git力量,因为他们unsafeness和变异.

Von*_*onC 39

重要的是,它不应该做grep,sed,tail甚至ghc -e包装,只有真正的git的力量,因为他们unsafeness和变异.

这只适用于git瓷器命令(参见" 瓷器在Git中的含义是什么意思? ")

使用plumbing命令ls-remote,然后您就可以过滤其输出.

没有参数的ls-remote仍会列出远程HEAD:

git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote origin
8598d26b4a4bbe416f46087815734d49ba428523    HEAD
8598d26b4a4bbe416f46087815734d49ba428523    refs/heads/master
38325f657380ddef07fa32063c44d7d6c601c012    refs/heads/test_trap
Run Code Online (Sandbox Code Playgroud)

但是如果你只询问所述遥控器的头部:

git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote --heads origin
8598d26b4a4bbe416f46087815734d49ba428523    refs/heads/master
38325f657380ddef07fa32063c44d7d6c601c012    refs/heads/test_trap
Run Code Online (Sandbox Code Playgroud)

最终答案:

git@vonc-VirtualBox:~/ce/ce6/.git$ git ls-remote --heads origin  | sed 's?.*refs/heads/??'
master
test_trap
Run Code Online (Sandbox Code Playgroud)

(是的,它使用sed,但是管道命令的输出应该足够稳定以便解析)

  • 请注意,这需要网络访问(git branch --remote --list不需要) (5认同)
  • 我也可以回答这个问题,但是"没有sed"规则会发生什么?大声笑,无论如何 (3认同)
  • @KurzedMetal是的,但我通过仅使用管道命令而不是像`git branch`这样的瓷器命令,小心地证明了使用`sed`.例如,参见http://stackoverflow.com/questions/2978947/how-to-list-all-the-log-for-current-project-in-git/2979124#2979124或http://stackoverflow.com/questions/2976665 /混帐的changelog一天按一天/ 2976776#2976776 (3认同)

Phi*_*ley 8

经过对相同问题的研究后,另一种方法是:

git for-each-ref --format='%(refname:strip=2)' refs/remotes/<remote_name>
Run Code Online (Sandbox Code Playgroud)

这将在您上次获取的点给出命名远程的本地引用的排序列表。

您可以针对他们的标签等进行调整。