简体   繁体   English

git:列出悬空标签

[英]git: list dangling tags

Context: 语境:

  • assume you have some rather tricky CI/CD workflow which relies on git tags 假设你有一些相当棘手的CI / CD工作流,它依赖于git标签
  • the feature branches are built and generate some short-lived tags to signify commits which yield the deployable artifacts 构建功能分支并生成一些短期标记,以表示产生可部署工件的提交
  • when the feature branch gets squash-merged, usually it's deleted, but the tags, unsurprisingly, survive 当功能分支被压缩合并时,通常会被删除, 但不出所料,标签会存活下来
  • after, say, several months of development the tag listing predictably becomes hairy 比如说,经过几个月的开发,标签列表可以预测会变得毛茸茸

Hence, the question: 因此,问题是:

how would I, using git command line and, optionally, basic bash tooling 我将如何使用git命令行和可选的基本bash工具

  1. list all the branches which have given tag reachable (the dual to that is git tag -l --merged ${BRANCH_COMMITTISH} , but I need not tags for the given branch but branches for a given tag) 列出所有给出标签可到达的分支(双重到git tag -l --merged ${BRANCH_COMMITTISH} ,但我不需要给定分支的标签,但给定标签的分支)
  2. list all the tags which have empty output from point 1 above (obviously this is doable with a for loop (given any terminating implementation for 1) , but maybe there's some neat magical git one-liner)? 列出所有从上面的第1点有空输出的标签(显然这对于​​for循环是可行的(给定1的任何终止实现) ,但也许有一些整洁的魔法git one-liner)?
git log --simplify-by-decoration --tags --not --branches --remotes --pretty=%d

--simplify-by-decoration says only show the bare minimum needed to reveal the ancestry (usually you use this with --graph ). --simplify-by-decoration表示只显示揭示祖先所需的最低限度(通常你用--graph )。 --tags --not --branches --remotes says, well, what it says: list the tag history that's not in the branches or remotes history, ie tags unreachable from any branch or remote-tracking branch. --tags --not --branches --remotes说,好吧,它说的是:列出不在分支或遥控器历史记录中的标记历史记录,即从任何分支或远程跟踪分支无法访问的标记。 --pretty=%d says just show the refs. --pretty=%d说只显示引用。

Just to illustrate all solutions I've seen so far: 只是为了说明到目前为止我看到的所有解决方案:

~$ 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM