如何获取 Git 提交计数?

发布于 2024-07-15 09:03:52 字数 294 浏览 5 评论 0原文

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

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

我目前在 Unix/Cygwin/msysGit 上确实喜欢这样做:

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

但我觉得这有点像黑客。

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

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

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

I currently do like that, on 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 wc or even Git, so it could work on a bare Windows. Just read a file or a directory structure...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(26

浪漫人生路 2024-07-22 09:03:52

要获取修订版的提交计数(HEADmaster、提交哈希):

git rev-list --count <revision>

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

git rev-list --count --all

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

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

git rev-list --count <revision>

To get the commit count across all branches:

git rev-list --count --all

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.

累赘 2024-07-22 09:03:52

git Shortlog 是一种方法。

git shortlog is one way.

垂暮老矣 2024-07-22 09:03:52

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

git shortlog -s

输出:

14 John lennon
9  Janis Joplin

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

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 -s argument is the contraction form of --summary.

岁月蹉跎了容颜 2024-07-22 09:03:52

git rev-list HEAD --count

git rev-list

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

--count :打印一个数字,说明已列出的提交数量,并抑制所有其他输出。

git rev-list HEAD --count

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).

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

死开点丶别碍眼 2024-07-22 09:03:52

如果您正在寻找一个唯一且仍然可读的提交标识符,git describe 可能正适合您。

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

痴情换悲伤 2024-07-22 09:03:52

您可以只使用:

git shortlog -s -n

结果:

     827  user one
        15  user two
         2  Gest 

You can just use :

git shortlog -s -n

Result :

     827  user one
        15  user two
         2  Gest 
溺孤伤于心 2024-07-22 09:03:52

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

“修订号”对于 Subversion 特别重要,因为它在合并时需要 (SVN1.5 1.6 在这方面有所改进)。

您最终可能会得到一个预提交挂钩,其中在注释中包含修订号,并使用算法不涉及查找分支的所有历史来确定正确的号码。

Bazaar 实际上想出了这样的算法,它可能是您想要的一个很好的起点去做。

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


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

这样,关于框将如下所示:

About box

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

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.

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).

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

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

(As Bombe's answer points 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.


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

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 applicative build number to a technical revision id.

您的好友蓝忘机已上羡 2024-07-22 09:03:52

一个简单的方法是:

 git log --oneline | wc -l

oneline 确保这一点。

A simple way is:

 git log --oneline | wc -l

oneline ensures that.

度的依靠╰つ 2024-07-22 09:03:52

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

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

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

export GIT_REV_COUNT=`git rev-list --all --count`
香橙ぽ 2024-07-22 09:03:52

Git Shortlog 是获取提交详细信息的一种方法:

git shortlog -s -n

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

Git shortlog is one way to get the commit details:

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.

迷离° 2024-07-22 09:03:52

如果您只使用一个分支,例如 master,我认为这会很好用:

git rev-list --full-history --all | wc -l

这只会输出一个数字。 您可以将其别名为类似的名称,

git revno

以使事情变得非常方便。 为此,请编辑您的 .git/config 文件并将其添加到:

[alias]
    revno = "!git rev-list --full-history --all | wc -l"

这在 Windows 上不起作用。 我不知道该操作系统对应的“wc”,但编写一个 Python 脚本来为您进行计数将是一个多平台解决方案。

编辑:获取两次提交之间的计数:


我一直在寻找一个答案来显示如何获取两个任意修订之间的提交数量,但没有看到任何答案。

git rev-list --count [older-commit]..[newer-commit]

If you're just using one branch, such as master, I think this would work great:

git rev-list --full-history --all | wc -l

This will only output a number. You can alias it to something like

git revno

to make things really convenient. To do so, edit your .git/config file and add this in:

[alias]
    revno = "!git rev-list --full-history --all | wc -l"

This will not work on Windows. I do not know the equivalent of "wc" for that OS, but writing a Python script to do the counting for you would be a multi-platform solution.

EDIT: Get count between two commits:


I was looking for an answer that would show how to get the number of commits between two arbitrary revisions and didn't see any.

git rev-list --count [older-commit]..[newer-commit]
ゃ人海孤独症 2024-07-22 09:03:52

有几种很酷的方法可以做到这一点 -

  • 第一种方法

git Shortlog -s

此命令打印对存储库做出贡献的所有用户的提交计数列表。

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

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

git Shortlog -s | grep "Pankaj Tanwar"

它打印 -

956 Pankaj Tanwar
  • 另一个干净而酷的方法是 -
git rev-list HEAD --author="Pankaj Tanwar" --count 

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

there are couple of cool methods to do so -

  • First method

git shortlog -s

This command prints a list of commits count by all users who contributed to the repo.

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

Simply, to get the count of total commits -

git shortlog -s | grep "Pankaj Tanwar"

it prints -

956 Pankaj Tanwar
  • Another clean and cool method is -
git rev-list HEAD --author="Pankaj Tanwar" --count 

To calculate total lines of code contributed & total pull requests raised, check this blog

血之狂魔 2024-07-22 09:03:52

Git 人员使用一个很好的帮助程序脚本来帮助生成基于 Git 描述的有用版本号。 我展示了脚本并在对 如何将当前提交 ID 包含在 Git 项目的文件中?

There's a nice helper script that the Git folks use to help generate a useful version number based on Git describe. I show the script and explain it in my answer to How would you include the current commit id in a Git project's files?.

折戟 2024-07-22 09:03:52

以下命令打印当前分支上的提交总数。

git shortlog -s -n  | awk '{ sum += $1; } END { print sum; }' "$@"

它由两部分组成:

  1. 打印按作者分组的日志总数 (git Shortlog -s -n)

    输出示例

    <前><代码> 1445 约翰 C
    第1398章
    第1376章
    166 贾斯汀 T
    166 你

  2. 将每个作者的总提交数(即每行的第一个参数)相加,并打印结果out (awk '{ sum += $1; } END { print sum; }' "$@")

    使用与上面相同的示例,其总和将为1445 + 1398 + 1376 + 166 + 166。 因此输出将是:

    <前><代码> 4,551

The following command prints the total number of commits on the current branch.

git shortlog -s -n  | awk '{ sum += $1; } END { print sum; }' "$@"

It is made up of two parts:

  1. Print the total logs number grouped by author (git shortlog -s -n)

    Example output

      1445  John C
      1398  Tom D
      1376  Chrsitopher P
       166  Justin T
       166  You
    
  2. Sum up the total commit number of each author, i.e. the first argument of each line, and print the result out (awk '{ sum += $1; } END { print sum; }' "$@")

    Using the same example as above it will sum up 1445 + 1398 + 1376 + 166 + 166. Therefore the output will be:

      4,551
    
歌入人心 2024-07-22 09:03:52

git rev-parse --短头

git rev-parse --short HEAD

还如梦归 2024-07-22 09:03:52

使用 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 的信息。 显然,只有当您最多基于给定的 master 修订版的一个分支时,这才有用。

可以将 --first-parent 添加到微编号中,以抑制仅因合并其他分支而产生的某些提交,尽管这可能是不必要的。

Using Bash syntax,

$(git rev-list --count HEAD)

looks fine for purely linear history. If you also want to sometimes have “numbers” from branches (based off master), consider:

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

When run from a checkout of master, you get simply 1234.0 or the like. When run from a checkout of a branch you will get something like 1234.13, if there have been 13 commits made on that branch. Obviously this is useful only insofar as you are basing at most one branch off a given master revision.

--first-parent could be added to the micro number to suppress some commits arising only from merging other branches, though it is probably unnecessary.

世俗缘 2024-07-22 09:03:52

在构建过程中生成一个数字并将其写入文件。 每当您发布版本时,请使用注释“Build 147”(或当前的任何内部版本号)提交该文件。 在正常开发期间不要提交该文件。 这样,您就可以轻松地在 Git 中的内部版本号和版本之间进行映射。

Generate a number during the build and write it to a file. Whenever you make a release, commit that file with the comment "Build 147" (or whatever the build number currently is). Don't commit the file during normal development. This way, you can easily map between build numbers and versions in Git.

痴者 2024-07-22 09:03:52

在我们公司,我们从 SVN 迁移到 Git。 缺少修订号是一个大问题!

执行 git svn clone ,然后通过 SVN 修订号标记最后一次 SVN 提交:

export hr=`git svn find-rev HEAD`
git tag "$hr" -f HEAD

然后您可以借助以下命令获取修订号

git describe --tags --long

此命令给出如下内容:

7603-3-g7f4610d

意味着: 最后一个标记是 7603 - 它是SVN 修订版。 3 - 是其中的提交计数。 我们需要添加它们。

因此,可以通过此脚本来计算修订版本号:

expr $(git describe --tags --long | cut -d '-' -f 1) + $(git describe --tags --long | cut -d '-' -f 2)

In our company, we moved from SVN to Git. Lack of revision numbers was a big problem!

Do git svn clone, and then tag the last SVN commit by its SVN revision number:

export hr=`git svn find-rev HEAD`
git tag "$hr" -f HEAD

Then you can get the revision number with help of

git describe --tags --long

This command gives something like:

7603-3-g7f4610d

Means: The last tag is 7603 - it's the SVN revision. 3 - is count of commits from it. We need to add them.

So, the revision number can be counted by this script:

expr $(git describe --tags --long | cut -d '-' -f 1) + $(git describe --tags --long | cut -d '-' -f 2)
徒留西风 2024-07-22 09:03:52

git Shortlog 本身并不能解决提交总数的原始问题(未按作者分组)

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

但是,在 Git 2.29(2020 年第 4 季度)中,“git Shortlog(man)< /sup> 变得更加精确。
它被教导按预告片行的内容对提交进行分组,例如“Reviewed-by:”、“Coauthored-by:”等。

请参阅提交 63d24fa提交 56d5dde, 提交 87abb96提交 f17b0b9提交47beb37提交 f0939a0提交 92338c4 (27 2020 年 9 月),以及提交 45d93eb(2020 年 9 月 25 日) https://github.com/peff" rel="nofollow noreferrer">杰夫·金 (peff)。
(由 Junio C Hamano -- gitster -- 合并于 提交 2fa8aac,2020 年 10 月 4 日)

shortlog:允许指定多个组

签字人:杰夫·金

现在,shortlog 支持从预告片中读取内容,因此合并来自多个预告片或预告片和作者之间的计数会非常有用。
这可以通过对多次运行的输出进行后处理来手动完成,但确保每个名称/提交对仅计算一次并不简单。

此补丁教导 Shortlog 在命令行上接受多个 --group 选项,并从所有选项中提取数据。

这使得可以运行:

git Shortlog -ns --group=author --group=trailer:co-authored-by   
  

获得对作者和共同作者同等计数的短日志。

实现基本上很简单。 “group”枚举变为位字段,预告片键变为列表。
我没有费心实现从标准输入读取的多组语义。 这是可以做到的,但现有的匹配代码使它变得尴尬,我怀疑有人关心。

我们用于预告片的重复抑制现在也涵盖了作者和提交者(尽管在非预告片单组模式下,我们可以跳过哈希插入和查找,因为每次提交我们只能看到一个值)。

有一个微妙之处:我们现在关心没有设置组位的情况(在这种情况下我们默认显示作者)。
builtin/log.c< 中的调用者/a> 需要进行调整以明确询问作者,而不是依赖 shortlog_init()。 通过一些体操可以使其继续按原样工作,但对于单个调用者来说这是不值得的。


git Shortlog 现在包含在其 手册页

--group=<类型>

基于分组提交。 如果没有 --group 选项
指定,默认为author 是以下之一:

  • 作者,提交按作者分组
  • 提交者,提交按提交者分组(与-c相同)

这是 --group=committer 的别名。

git Shortlog 现在也包含在其 手册页

如果多次指定--group,则每次都会对提交进行计数
值(但同样,该提交中的每个唯一值仅一次)。 为了
例如, git Shortlog --group=author --group=trailer:co-authored-by
包括作者和共同作者。

git shortlog by itself does not address the original question of total number of commits (not grouped by author)

That is true, and git rev-list HEAD --count remains the simplest answer.

However, with Git 2.29 (Q4 2020), "git shortlog"(man) has become more precise.
It has been taught to group commits by the contents of the trailer lines, like "Reviewed-by:", "Coauthored-by:", etc.

See commit 63d24fa, commit 56d5dde, commit 87abb96, commit f17b0b9, commit 47beb37, commit f0939a0, commit 92338c4 (27 Sep 2020), and commit 45d93eb (25 Sep 2020) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 2fa8aac, 04 Oct 2020)

shortlog: allow multiple groups to be specified

Signed-off-by: Jeff King

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 now includes in its man page:

--group=<type>

Group commits based on <type>. If no --group option is
specified, the default is author. <type> is one of:

  • author, commits are grouped by author
  • committer, commits are grouped by committer (the same as -c)

This is an alias for --group=committer.

git shortlog now also includes in its man page:

If --group is specified multiple times, commits are counted under each
value (but again, only once per unique value in that commit). For
example, git shortlog --group=author --group=trailer:co-authored-by
counts both authors and co-authors.

时光匆匆的小流年 2024-07-22 09:03:52

我以前使用的方法是:

git log | grep "^commit" | wc -l

简单但有效。

The one I used to use was:

git log | grep "^commit" | wc -l

Simple but it worked.

微暖i 2024-07-22 09:03:52

您可以尝试

git log --oneline | wc -l

列出存储库中贡献者所做的所有提交

git shortlog -s

You can try

git log --oneline | wc -l

or to list all the commits done by the people contributing in the repository

git shortlog -s
鸵鸟症 2024-07-22 09:03:52

git config --global alias.count 'rev-list --all --count'

如果将其添加到配置中,则只需引用该命令即可;

git 计数

git config --global alias.count 'rev-list --all --count'

If you add this to your config, you can just reference the command;

git count

娇柔作态 2024-07-22 09:03:52

创建一个别名怎么样?

alias gc="git rev-list --all --count"      #Or whatever name you wish

How about making an alias ?

alias gc="git rev-list --all --count"      #Or whatever name you wish
我的奇迹 2024-07-22 09:03:52

要获取两个分支(例如功能分支和目标分支)之间不同的提交数量,请使用:

git rev-list --count feature_branch..target_branch

To get the number of commits that differ between two branches e.g. a feature branch and a target use:

git rev-list --count feature_branch..target_branch

街角迷惘 2024-07-22 09:03:52

您可以在 git bash 中尝试这些

  1. git log --author="Your-Email-or-Name" --pretty=oneline

这会给您一个列表

  1. git log --author="Your -电子邮件或姓名”--pretty=oneline | wc -l <​​/code>

这会给你一个计数

You can try these in git bash

  1. git log --author="Your-Email-or-Name" --pretty=oneline

this gives you a list

  1. git log --author="Your-Email-or-Name" --pretty=oneline | wc -l

this gives you a count

天赋异禀 2024-07-22 09:03:52

像这样使用 git Shortlog

git Shortlog -sn

或者创建一个别名(对于基于 ZSH 的终端)

# 按提交显示贡献者
别名 gcall="git Shortlog -sn"

Use git shortlog just like this

git shortlog -sn

Or create an alias (for ZSH based terminal)

# show contributors by commits
alias gcall="git shortlog -sn"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文