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

我们的目标是将其用作唯一的、递增的构建号。

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

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

但我觉得它有点过时了。

有更好的办法吗?如果我实际上不需要wc甚至Git,那么它就可以在裸Windows上工作,那就太棒了。只要读取一个文件或目录结构…


当前回答

Git shortlog本身并没有解决提交总数的原始问题(没有按作者分组)

这是事实,git rev-list HEAD—count仍然是最简单的答案。

然而,随着Git 2.29(2020年Q4)的出现,“Git shortlog”(man)变得更加精确。 它已经被教导根据预告行内容来分组提交,比如“审阅者:”,“合著者:”,等等。

参见Jeff King (peff)的commit 63d24fa、commit 56d5dde、commit 87abb96、commit f17b0b9、commit 47beb37、commit f0939a0、commit 92338c4(2020年9月27日)和commit 45d93eb(2020年9月25日)。 (由Junio C Hamano—gitster—在commit 2fa8aac中合并,2020年10月4日)

Shortlog:允许指定多个组 署名:杰夫·金

Now that shortlog supports reading from trailers, it can be useful to combine counts from multiple trailers, or between trailers and authors. This can be done manually by post-processing the output from multiple runs, but it's non-trivial to make sure that each name/commit pair is counted only once. This patch teaches shortlog to accept multiple --group options on the command line, and pull data from all of them. That makes it possible to run: git shortlog -ns --group=author --group=trailer:co-authored-by to get a shortlog that counts authors and co-authors equally. The implementation is mostly straightforward. The "group" enum becomes a bitfield, and the trailer key becomes a list. I didn't bother implementing the multi-group semantics for reading from stdin. It would be possible to do, but the existing matching code makes it awkward, and I doubt anybody cares. The duplicate suppression we used for trailers now covers authors and committers as well (though in non-trailer single-group mode we can skip the hash insertion and lookup, since we only see one value per commit). There is one subtlety: we now care about the case when no group bit is set (in which case we default to showing the author). The caller in builtin/log.c needs to be adapted to ask explicitly for authors, rather than relying on shortlog_init(). It would be possible with some gymnastics to make this keep working as-is, but it's not worth it for a single caller.

Git shortlog现在包含在它的手册页中:

——组= <类型> 分组提交基于<类型>。如果没有——group选项为 指定时,默认为author。<type>是其中之一: 作者,提交按作者分组 提交器,提交按提交器分组(与-c相同) 这是——group=committer的别名。

Git短日志现在还包括在它的手册页:

如果多次指定——group,则每次提交都被计数 值(但同样,在该提交中只对每个惟一值执行一次)。为 例如,git shortlog——group=author——group=trailer:co-author -by 包括作者和合著者。

其他回答

如果您正在为提交寻找一个唯一且可读性强的标识符,那么git describe可能正是适合您的东西。

有一些很酷的方法可以做到这一点

第一个方法

git shortlog -s

该命令打印所有参与repo的用户的提交计数列表。

956 Pankaj Tanwar
235 The Ninja
540 The Hardcore Geek
664 The Ever Shining Star
984 The Experienced Man

简单地说,要获得总提交数-

git shortlog -s |抓住“潘卡伊·坦瓦尔”

它打印-

956 Pankaj Tanwar

另一种干净又酷的方法是-

git rev-list HEAD --author="Pankaj Tanwar" --count 

要计算贡献的代码行数和提出的拉请求总数,请查看此博客

使用Bash语法,

$(git rev-list --count HEAD)

对于纯线性历史来说还好。如果你有时也想从分支中获得“数字”(基于master),考虑:

$(git rev-list --count $(git merge-base master HEAD)).$(git rev-list --count ^master HEAD)

当从master签出运行时,您得到的只是1234.0或类似的结果。当从分支的签出运行时,如果在该分支上已经进行了13次提交,那么您将得到类似于1234.13的结果。显然,这只在你基于一个给定的主修订的最多一个分支时有用。

first-parent可以加入到微号中,以抑制一些合并其他分支时产生的提交,尽管这可能是不必要的。

Git shortlog本身并没有解决提交总数的原始问题(没有按作者分组)

这是事实,git rev-list HEAD—count仍然是最简单的答案。

然而,随着Git 2.29(2020年Q4)的出现,“Git shortlog”(man)变得更加精确。 它已经被教导根据预告行内容来分组提交,比如“审阅者:”,“合著者:”,等等。

参见Jeff King (peff)的commit 63d24fa、commit 56d5dde、commit 87abb96、commit f17b0b9、commit 47beb37、commit f0939a0、commit 92338c4(2020年9月27日)和commit 45d93eb(2020年9月25日)。 (由Junio C Hamano—gitster—在commit 2fa8aac中合并,2020年10月4日)

Shortlog:允许指定多个组 署名:杰夫·金

Now that shortlog supports reading from trailers, it can be useful to combine counts from multiple trailers, or between trailers and authors. This can be done manually by post-processing the output from multiple runs, but it's non-trivial to make sure that each name/commit pair is counted only once. This patch teaches shortlog to accept multiple --group options on the command line, and pull data from all of them. That makes it possible to run: git shortlog -ns --group=author --group=trailer:co-authored-by to get a shortlog that counts authors and co-authors equally. The implementation is mostly straightforward. The "group" enum becomes a bitfield, and the trailer key becomes a list. I didn't bother implementing the multi-group semantics for reading from stdin. It would be possible to do, but the existing matching code makes it awkward, and I doubt anybody cares. The duplicate suppression we used for trailers now covers authors and committers as well (though in non-trailer single-group mode we can skip the hash insertion and lookup, since we only see one value per commit). There is one subtlety: we now care about the case when no group bit is set (in which case we default to showing the author). The caller in builtin/log.c needs to be adapted to ask explicitly for authors, rather than relying on shortlog_init(). It would be possible with some gymnastics to make this keep working as-is, but it's not worth it for a single caller.

Git shortlog现在包含在它的手册页中:

——组= <类型> 分组提交基于<类型>。如果没有——group选项为 指定时,默认为author。<type>是其中之一: 作者,提交按作者分组 提交器,提交按提交器分组(与-c相同) 这是——group=committer的别名。

Git短日志现在还包括在它的手册页:

如果多次指定——group,则每次提交都被计数 值(但同样,在该提交中只对每个惟一值执行一次)。为 例如,git shortlog——group=author——group=trailer:co-author -by 包括作者和合著者。

一个简单的方法是:

 git log --oneline | wc -l

Oneline确保了这一点。