【问题标题】:How to filter branch name with keyword?如何用关键字过滤分支名称?
【发布时间】:2021-12-23 22:55:10
【问题描述】:

我正在使用此逻辑,并且需要与此字符串“AMAZONFIX”匹配的唯一分支名称。

git for-each-ref --format='%(committerdate:short) %(authordate:relative) %(refname:lstrip=3)' --sort -committerdate refs/remotes/$REMOTE_NAME | while read date branch; do
printf "%s | %s %s %s %s %s | %50s\n" $date $branch

输出结果:

feature/core-xyz
dummy/AMAZON-bitMap
bugfix/AMAZONFIX-work
main/outlet-file

异常输出:

dummy/AMAZONFIX-bitMap
bugfix/work-place-AMAZONFIX

仅具有“AMAZONFIX”的分支名称

【问题讨论】:

  • 通过 grep 将其用于 AMAZONFIX?

标签: git git-branch git-bash


【解决方案1】:

如果您不想使用像| grep AMAZONFIX 这样的后处理步骤,请考虑使用git for-each-ref --contains=... 指令(与Git 2.7, Q4 2015 一起引入)

在你的情况下,为了可读性,多行:

git for-each-ref \
  --format='%(committerdate:short) %(authordate:relative) 
            %(refname:lstrip=3)' 
  --contains=$REMOTE_NAME/AMAZONFIX --contains=AMAZONFIX 
  --sort -committerdate refs/remotes/origin | \
  while read date branch; \
    do printf "%s | %s %s %s %s %s | %50s\n" $date $branch; 
  done

但是,这只适用于名为 AMAZONFIX 的分支,而不适用于 xxxAMAZONFIXyyy

由于--contains 不支持通配符,grep 仍然是最佳选择:

git for-each-ref...| while... ;done | grep AMAZONFIX

【讨论】:

  • 我尝试了相同但得到错误为:错误:错误的对象名称来源/AMAZONFIX @VonC​​pan>
  • @rowoc 确实:它适用于我,但只是因为我有一个名为 AMAZONFIX 的分支。在您的情况下, grep 仍然是最佳选择。我已经相应地编辑了答案。
  • 谢谢@VonC​​pan>
猜你喜欢
  • 2012-03-03
  • 2013-12-26
  • 2018-11-20
  • 2020-06-22
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多