代码之家  ›  专栏  ›  技术社区  ›  Anton Kraievyi

git:列出悬挂的标记

  •  2
  • Anton Kraievyi  · 技术社区  · 6 年前

    上下文:

    • 假设您有一些相当复杂的CI/CD工作流,它依赖于git标记
    • 构建特征分支并生成一些 短暂的 用于表示提交的标记,该提交产生可部署的构件
    • 当特征分支被挤压合并时,通常会被删除, 但毫无疑问,这些标签幸存了下来
    • 比如说,经过几个月的开发,标签列表可以预见地变得毛茸茸的

    因此,问题是:

    如何使用git命令行和基本bash工具

    1. 列出所有给定标记可访问的分支(即 git tag -l --merged ${BRANCH_COMMITTISH} ,但我不需要给定分支的标记,而是需要给定标记的分支)
    2. 列出上面第1点中所有输出为空的标记(显然这可以通过for循环实现 (考虑到1的任何终止实现) ,但也许有一些漂亮的魔法吉特一行?
    3 回复  |  直到 6 年前
        1
  •  3
  •   jthill    6 年前
    git log --simplify-by-decoration --tags --not --branches --remotes --pretty=%d
    

    --simplify-by-decoration 表示只显示显示祖先所需的最小值(通常与 --graph ). --tags --not --branches --remotes 说明:列出不在分支或远程历史记录中的标记历史记录,即无法从任何分支或远程跟踪分支访问的标记。 --pretty=%d 说只要给裁判看就行了。

        2
  •  3
  •   phd    6 年前
        3
  •  0
  •   Anton Kraievyi    6 年前

    只是为了说明到目前为止我看到的所有解决方案:

    ~$ git init dangling_tags
    Initialized empty Git repository in ~/dangling_tags/.git/
    $ cd dangling_tags/
    ~/dangling_tags$ touch a.txt && git add a.txt && git commit -m a
    [master (root-commit) f1b1070] a
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 a.txt
    ~/dangling_tags$ git tag a
    ~/dangling_tags$ git checkout -b feature/add_some_tags
    Switched to a new branch 'feature/add_some_tags'
    ~/dangling_tags$ touch b.txt && git add b.txt && git commit -m b 
    [feature/add_some_tags 1871cde] b
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 b.txt
    ~/dangling_tags$ git tag b
    ~/dangling_tags$ touch c.txt && git add c.txt && git commit -m c 
    [feature/add_some_tags 26f6611] c
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 c.txt
    ~/dangling_tags$ git tag c
    ~/dangling_tags$ git checkout master
    Switched to branch 'master'
    ~/dangling_tags$ git merge --squash feature/add_some_tags 
    Updating f1b1070..26f6611
    Fast-forward
    Squash commit -- not updating HEAD
     b.txt | 0
     c.txt | 0
     2 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 b.txt
     create mode 100644 c.txt
    ~/dangling_tags$ git commit
    [master 99b33ae] Squashed commit of the following:
     2 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 b.txt
     create mode 100644 c.txt
    ~/dangling_tags$ git branch --contains a
    * master
    ~/dangling_tags$ git branch --contains b
      feature/add_some_tags 
    ~/dangling_tags$ git branch -D feature/add_some_tags 
    Deleted branch feature/add_some_tags (was 26f6611).
    ~/dangling_tags$ git tag
    a
    b
    c
    ~/dangling_tags$ git branch --contains a
    * master
    ~/dangling_tags$ git branch --contains b
    ~/dangling_tags$ for t in $(git tag); do if test "$(git branch --contains $t | wc -l)" == "0" ; then echo $t; fi done
    b
    c
    ~/dangling_tags$ git log --simplify-by-decoration --tags --not --branches --remotes --pretty=%d
     (tag: c)
     (tag: b)