如何获得 Git 提交计数?

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

How do I get the Git commit count?

gitbuild-processrevision

提问by Splo

I'd like to get the number of commits of my Git repository, a bit like SVN revision numbers.

我想获得我的 Git 存储库的提交次数,有点像 SVN 修订号。

The goal is to use it as a unique, incrementing build number.

目标是将其用作唯一的、递增的内部版本号。

I currently do like that, on Unix/Cygwin/msysGit:

我目前喜欢这样,在 Unix/Cygwin/msysGit 上:

git log --pretty=format:'' | wc -l

But I feel it's a bit of a hack.

但我觉得这有点像黑客。

Is there a better way to do that? It would be cool if I actually didn't need wcor even Git, so it could work on a bare Windows. Just read a file or a directory structure...

有没有更好的方法来做到这一点?如果我真的不需要wc甚至不需要Git 那就太酷了,这样它就可以在裸机上运行。只需读取文件或目录结构...

回答by Benjamin Atkin

To get a commit count for a revision (HEAD, master, a commit hash):

要获取修订的提交计数(HEAD, master,提交哈希):

git rev-list --count <revision>

To get the commit count across all branches:

要获取所有分支的提交计数:

git rev-list --all --count

I recommend against using this for build identifier, but if you must, it's probably best to use the count for the branch you're building against. That way the same revision will always have the same number. If you use the count for all branches, activity on other branches could change the number.

我建议不要使用它作为构建标识符,但如果必须,最好使用您正在构建的分支的计数。这样,相同的修订版本将始终具有相同的编号。如果您对所有分支使用计数,则其他分支上的活动可能会更改该数量。

回答by Rayne

git shortlogis one way.

git shortlog是一种方式。

回答by Jake Berger

git rev-list HEAD --count

git rev-list HEAD --count

git rev-list

git rev-list

git rev-list <commit>: List commits that are reachable by following the parent links from the given commit (in this case, HEAD).

git rev-list <commit>:列出可通过遵循给定提交(在本例中为HEAD)中的父链接来访问的提交。

--count: Print a number stating how many commits would have been listed, and suppress all other output.

--count:打印一个数字,说明将列出多少提交,并禁止所有其他输出。

回答by Alex Pliutau

This command returns count of commits grouped by committers:

此命令返回按提交者分组的提交计数:

git shortlog -s

Output:

输出:

14 John lennon
9  Janis Joplin

You may want to know that the -sargument is the contraction form of --summary.

您可能想知道-s参数是 的收缩形式--summary

回答by Bombe

If you're looking for a unique and still quite readable identifier for commits, git describemight be just the thing for you.

如果您正在为提交寻找唯一且仍然可读的标识符,那么git describe可能正是您的选择。

回答by VonC

You are not the first one to think about a "revision number" in Git, but 'wc' is quite dangerous, since commit can be erased or squashed, and the history revisited.

您不是第一个在 Git 中考虑“修订号”的人,但是 ' wc' 非常危险,因为提交可以被擦除或压缩,并且可以重新访问历史。

The "revision number" was especially important for Subversion since it was needed in case of merge(SVN1.5 and 1.6 have improved on that front).

“修订号”对于 Subversion 尤其重要,因为在合并的情况下需要它(SVN1.5 和 1.6 在这方面有所改进)。

You could end up with a pre-commit hook which would include a revision number in the comment, with an algorithm not involvinglooking up the allhistory of a branch to determine the correct number.

您可能会得到一个 pre-commit 钩子,它会在注释中包含一个修订号,算法不涉及查找分支的所有历史记录以确定正确的编号。

Bazaaractually came up with such an algorithm , and it may be a good starting point for what you want to do.

Bazaar居然想出了这样一个算法,它可能是你想做的一个很好的起点。

(As Bombe's answerpoints out, Git has actually an algorithm of its own, based on the latest tag, plus the number of commits, plus a bit of an SHA-1 key). You should see (and upvote) his answer if it works for you.

(正如Bombe 的回答所指出的,Git 实际上有一个自己的算法,基于最新的标签,加上提交的数量,再加上一点 SHA-1 密钥)。如果对您有用,您应该看到(并投票)他的答案。



To illustrate Aaron's idea, you can also append the Git commit hash into an application's "info" fileyou are distributing with your application.

为了说明Aaron 的想法,您还可以将 Git 提交哈希附加到与应用程序一起分发的应用程序的“信息”文件中。

That way, the about box would look like:

这样,关于框看起来像:

About box

关于框

The applicative number is part of the commit, but the 'application's "info" file' is generated during the packaging process, effectively linking an applicativebuild number to a technical revision id.

应用编号是提交的一部分,但“应用程序的“信息”文件是在打包过程中生成的,有效地将应用版本号链接到技术修订id

回答by demenvil

U can just use :

你可以使用:

git shortlog -s -n

Result :

结果 :

 827  user one
    15  user two
     2  Gest 

回答by Jimmy Kane

A simple way is:

一个简单的方法是:

 git log --oneline | wc -l

onelineensures that.

oneline确保。

回答by John Gietzen

To get it into a variable, the easiest way is:

要将其放入变量,最简单的方法是:

export GIT_REV_COUNT=`git rev-list --all --count`

回答by Sri Murthy Upadhyayula

Git shortlog is one way to get the commit details:

Git shortlog 是获取提交详细信息的一种方式:

git shortlog -s -n

This will give the number of commits followed by the author name. The -s option removes all the commit messages for each commit that the author made. Remove the same option if you would like to see the commit messages also. The -n option is used for sorting the entire list. Hope this helps.

这将给出提交次数,后跟作者姓名。-s 选项删除作者所做的每个提交的所有提交消息。如果您还想查看提交消息,请删除相同的选项。-n 选项用于对整个列表进行排序。希望这可以帮助。