如何使用标记后的提交次数显示 git commit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8595391/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:16:30  来源:igfitidea点击:

How to show git commit using number of commits since a tag

git

提问by Joel

With git describeyou can get the number of commits since the last tag. If you only had the tag and the number of commits what is the best way to show the commit that was described?

有了git describe你可以得到提交的数目自上次标签。如果您只有标签和提交次数,那么显示所描述的提交的最佳方式是什么?

I know you could use git log tag..and pipe it to a a script that does the counting but I was hoping for a more elegant solution similar to git show tag~n.

我知道您可以使用git log tag..它并将其通过管道传输到进行计数的脚本,但我希望有一个更优雅的解决方案,类似于git show tag~n.

To add more context, we are planning using git describeto create release numbers, for example with

为了添加更多上下文,我们计划使用git describe创建版本号,例如

$ git describe
v1.5-39-g5ede964

we would use foo_1.5.39. What we would like to do is knowing 1.5.39 means the 39th commit after the v1.5 tag, find that commit, i.e. find g5ede964. As pointed out in a comment, the 39th commit after v1.5 may not be unique. So perhaps a better way to ask this is what is the best way to find all commits X such that if HEAD was pointing to X git describewould return v1.5-39-*****.

我们将使用 foo_1.5.39。我们想要做的是知道 1.5.39 表示 v1.5 标签之后的第 39 次提交,找到那个提交,即找到 g5ede964。正如评论中指出的那样,v1.5 之后的第 39 次提交可能不是唯一的。因此,也许更好的提问方式是找到所有提交 X 的最佳方法是什么,以便如果 HEAD 指向 Xgit describe将返回 v1.5-39-*****

采纳答案by Lily Ballard

What you're asking for is impossible in the general case. The number of commits alone can't tell you anything if there are any merges in your history.

在一般情况下,您要求的内容是不可能的。如果您的历史记录中有任何合并,仅提交的数量并不能告诉您任何信息。

For example, given the following repo structure:

例如,给定以下 repo 结构:

a - b - c - d - h
  \           /
    e - f - g

With a tag put on a, the outputs of git describe dand git describe gare identical save for the SHA1:

加上标签a,除了 SHA1 之外,git describe d和的输出git describe g是相同的:

> git describe d
tag-3-ge8dca33
> git describe g
tag-3-g4fecc2e

That said, if you don't have a bunch of parallel branches going on at once, then you may be able to resolve a given commit number back to a single commit, but if you have even a single active side branch at the time of your tag then this may not work.

也就是说,如果您没有同时进行一堆并行分支,那么您可能能够将给定的提交编号解析回单个提交,但是如果您在执行时甚至只有一个活动侧分支你的标签那么这可能不起作用。

If you need reliable release numbers, you should stick to explicit tags.

如果你需要可靠的版本号,你应该坚持使用明确的标签。

回答by Sam

Try

尝试

git rev-list tag..HEAD --count

OR

或者

git rev-list tag.. --count

They mean the same thing.

他们的意思是一样的。

回答by Justin Ohms

If you are looking for the number of commits since the last tag, the following worked for me

如果您正在寻找自上一个标签以来的提交次数,以下内容对我有用

git rev-list  `git rev-list --tags --no-walk --max-count=1`..HEAD --count

回答by fge

You can:

你可以:

git log --oneline tag.. | wc -l

this will give you the number of commits

这会给你提交的数量

回答by PhilLab

As Kevin's answerexplained, this is generally not possible. To solve the problem for special cases he mentioned:

正如凯文的回答所解释的那样,这通常是不可能的。为了解决特殊情况的问题,他提到:

That said, if you don't have a bunch of parallel branches going on at once, then you may be able to resolve a given commit number back to a single commit, but if you have even a single active side branch at the time of your tag then this may not work.

也就是说,如果您没有同时进行一堆并行分支,那么您可能能够将给定的提交编号解析回单个提交,但是如果您在执行时甚至只有一个活动侧分支你的标签那么这可能不起作用。

you can use the following command (with nbeing the number of commits since the tag)

您可以使用以下命令(n自标记以来的提交次数)

git rev-list tag..HEAD --reverse | awk NR==n

回答by jthill

what is the best way to find all commits X such that if HEADwas pointing to X git describewould return v1.5-39-*****

找到所有提交 X 的最佳方法是什么,以便如果HEAD指向 Xgit describe将返回 v1.5-39-*****

is still the wrong question. The right question is

仍然是错误的问题。正确的问题是

how to find all commits X such that if HEAD was pointing to X an ordinary git describecould ever have returnedv1.5-39-*****

如何找到所有提交 X 以便如果 HEAD 指向 X 一个普通的git describe可以返回v1.5-39-*****

and it's not all that easy to answer that. Still, absent enemy action this procedure will list the right commit maybe among some other possibilities:

要回答这个问题并不容易。尽管如此,如果没有敌人的行动,这个程序将列出正确的提交,也许还有其他一些可能性:

ancestor=v1.5 n=39
git rev-list --all --reverse --topo-order --parents --ancestry-path ^$ancestor \
| awk ' function minmore(       i) {
                for ( i=2; i <= NF; ++i )
                        if ( gen[$i] > gen[] ) 
                                gen[]=gen[$i]
                return ++gen[]
        }
        minmore()<=n {
                print "[[ $(git rev-list --count "ancestor".."") == "n" ]] &&"
                print "         git describe --match="ancestor" " }
      ' ancestor=$ancestor n=$n \
# | sh    # lose the first `#` here to actually run the generated checks

The reason for the uncertainty and complexity is that git describeis just trying to get a reasonable base, so (see its docs) it'll pick the most recent of the closest current tags it can see, and count how many additional commits there are in the described history, and generate a moniker that way. For examples, new tags can change its preferred base such that a bare git describewould no longer generate that moniker; and while SHA's are immutable, references can be rewritten.

不确定性和复杂性的原因是它git describe只是试图获得一个合理的基础,因此(请参阅其文档)它将选择它可以看到的最近的最近标签,并计算有多少额外的提交在描述历史,并以这种方式生成一个绰号。例如,新标签可以更改其首选基础,从而使裸标签git describe不再生成该绰号;虽然 SHA 是不可变的,但可以重写引用。

回答by ctech 2030

This will give you the number of commits between these two commit.

这将为您提供这两次提交之间的提交次数。

$ git log --oneline 8a32722def6b80e343...e817c082323e65bb1053

$ git log --oneline 8a32722def6b80e343...e817c082323e65bb1053

回答by 1c0DevPi

Try the following, if you want to get the number of commits since last tag.

如果您想获取自上次标记以来的提交次数,请尝试以下操作。

git rev-list $(git describe --abbrev=0)..HEAD --count