计算Git分支上的提交次数


Answers:


349

要计算分支的提交,您需要:

git rev-list --count HEAD

为一个分支

git rev-list --count <branch-name>

如果要计算自创建分支以来在分支上进行的提交

git rev-list --count HEAD ^<branch-name>

这将计算所有曾经不在分支名称上的提交。

例子

git checkout master
git checkout -b test
<We do 3 commits>
git rev-list --count HEAD ^master

结果:3

如果您的分支来自一个名为的分支develop

git checkout develop
git checkout -b test
<We do 3 commits>
git rev-list --count HEAD ^develop

结果:3

忽略合并

如果您将另一个分支合并到当前分支而不进行快进,并且执行了上述操作,则合并也将被计入。这是因为对于git,合并是一个提交。

如果您不想计算这些提交,请添加--no-merges

git rev-list --no-merges --count HEAD ^develop

7
这些都不显示正确的编号,例如master和branchname显示相同的提交数量。
botbot

注释实际上并不允许代码,但这应该表明它确实有效。==== $ git init ==== $ touch test.txt ==== $ git add。==== $ git commit -a ==== $ git rev-list --count HEAD => 1 ==== $ git rev-list --count master => 1 ==== $ git checkout -b测试==== $ git rev-list --count测试=> 1 ==== $ git rev-list --count HEAD ^ master => 0 ==== $ touch test2.txt ==== $ git添加。==== $ git commit -a ==== $ git rev-list --count master => 1 ==== $ git rev-list --count test => 2 ==== $ git rev-list --count HEAD ^ master => 1 ====
彼得·范德

1
我同意@botbot。这些不是很准确。例如,尝试添加一些合并提交或拉取/重新设置,然后注意到上面描述的计数开始变得不可靠。
维尔摩尔三世

2
@wilmoore您的意思是说,合并分支后您会得到额外的计数吗?从技术上讲,这是一次提交,因此算在内。但是如果您不想计算这些提交,请添加--no-merges。我将更新答案。
彼得·范德

2
rev-list --count标志在git 1.7中不存在。目前,下面使用的令人讨厌的建议git log比其他建议效果更好。
aaronbauman '16

60

要查看未提交的总数,您可以按照彼得在上面的建议进行操作

git rev-list --count HEAD

如果您想查看每个人的提交次数,请尝试以下行

git shortlog -s -n

将产生这样的输出

135  Tom Preston-Werner
15  Jack Danger Canty
10  Chris Van Pelt
7  Mark Reid
6  remi

3
这些数字在名字前是什么?你可以解释吗 ?
Ciasto piekarz

5
@Ciastopiekarz这些是每个人的提交次数。
Asnad Atta

39

它可能需要相对较新的Git版本,但这对我来说很好:

git rev-list --count develop..HEAD

这为我提供了基于master的当前分支中的提交的准确计数。

Peter的答案中的命令git rev-list --count HEAD ^develop包括更多的提交,即我当前项目中的678 vs 97。

我的提交历史在该分支上是线性的,因此是YMMV,但它给出了我想要的确切答案,即“到目前为止,我已在该功能分支上添加了多少个提交?”。


应该是一样的。该文档是这么说的A special notation "<commit1>..<commit2>" can be used as a short-hand for "^'<commit1>' <commit2>". For example, either of the following may be used interchangeably: $ git rev-list origin..HEAD $ git rev-list HEAD ^origin
dantntmatter

我很困惑:git fetch upstream; BEHIND=$(git rev-list --count HEAD..upstream/master); git merge --ff-only upstream/master~$BEHIND;没有排队。BEHIND就像1800,但实际上没有什么比合并upstream / master〜400产生变化。使用--no-merges并没有更好,给出了900。如果我用〜800进行这样的合并,并且rev-list计数是1800,那么我用〜790进行了合并,我得到的转速降低了6到28之间-清单。
dlamblin

7

自历史记录开始以来,对当前分支进行了多少次提交,不包括来自合并分支的提交:

git rev-list HEAD --count --first-parent

从文档git rev-list --help

--first-parent

看到合并提交后,仅遵循第一个父提交。当查看特定主题分支的演变时,此选项可以提供更好的概述,因为合并到主题分支中的趋势往往只是不时调整以适应更新的上游,并且该选项使您可以忽略引入到其中的单个提交。通过这样的合并,您的历史记录。不能与--bisect结合使用。

注意:浅克隆将缩小历史记录大小。例如,如果您使用克隆--depth 1,则返回1。

其他提交以来完成的提交数量:

git rev-list HEAD abc0923f --count --first-parent

或相同:

git rev-list abc0923f.. --count --first-parent

或使用其他git参考

git rev-list master tag-v20 --count --first-parent

计算自2018年以来完成的提交

git rev-list HEAD --count --first-parent --since=2018-01-01

2018年1月1日,2018年1月1日,2018.01.01也有效。


git rev-label

我编写了一个脚本,以从Git获取版本修订的格式'$refname-c$count-g$short$_dirty',该格式扩展为master-c137-gabd32ef
脚本本身包含帮助。


git rev-list abc0923f .. --count --first-parent为我的分支提供了适当的结果,但first命令给出了很大的价值
Jiss Raphel

5

怎么样 git log --pretty=oneline | wc -l

从当前分支的角度来看,这应该算所有提交。


您算哪一栏?是第一个吗?
横街

3

我喜欢做git shortlog -s -n --all。为您提供名称和提交次数的“排行榜”样式列表。


2

一种方法是列出分支的日志并计数行数。

git log <branch_name> --oneline | wc -l

1

好吧,如果您将分支从非特定分支分支出来(即,不是masterdevelop),则所选答案将不起作用。

在这里,我提供了另一种在pre-pushgit钩子中使用的方式。

# Run production build before push
echo "[INFO] run .git/hooks/pre-push"

echo "[INFO] Check if only one commit"

# file .git/hooks/pre-push
currentBranch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

gitLog=$(git log --graph --abbrev-commit --decorate  --first-parent HEAD)

commitCountOfCurrentBranch=0
startCountCommit=""
baseBranch=""

while read -r line; do

    # if git log line started with something like "* commit aaface7 (origin/BRANCH_NAME)" or "commit ae4f131 (HEAD -> BRANCH_NAME)"
    # that means it's on our branch BRANCH_NAME

    matchedCommitSubstring="$( [[ $line =~ \*[[:space:]]commit[[:space:]].*\((.*)\) ]] && echo ${BASH_REMATCH[1]} )"

    if [[ ! -z ${matchedCommitSubstring} ]];then

      if [[  $line =~ $currentBranch ]];then
        startCountCommit="true"
      else
        startCountCommit=""

        if [[ -z ${baseBranch} ]];then
          baseBranch=$( [[ ${matchedCommitSubstring} =~ (.*)\, ]] && echo ${BASH_REMATCH[1]} || echo ${matchedCommitSubstring} )

        fi

      fi

    fi


    if [[ ! -z ${startCountCommit} && $line =~ ^\*[[:space:]]commit[[:space:]] ]];then
      ((commitCountOfCurrentBranch++))
    fi


done <<< "$gitLog"

if [[ -z ${baseBranch} ]];then

  baseBranch="origin/master"

else

  baseBranch=$( [[ ${baseBranch} =~ ^(.*)\, ]] && echo ${BASH_REMATCH[1]} || echo ${baseBranch} )

fi


echo "[INFO] Current commit count of the branch ${currentBranch}:  ${commitCountOfCurrentBranch}"

if [[ ${commitCountOfCurrentBranch} -gt 1 ]];then
  echo "[ERROR] Only a commit per branch is allowed. Try run 'git rebase -i ${baseBranch}'"
  exit 1
fi

有关更多分析,请访问我的博客


1

由于OP 在git中引用了分支上的提交数,因此我想补充一下,至少从git版本2.17.1开始,给定的答案也可以与任何其他分支一起使用(并且似乎比Peter van der的答案更可靠):

正常工作:

git checkout current-development-branch
git rev-list --no-merges --count master..
62
git checkout -b testbranch_2
git rev-list --no-merges --count current-development-branch..
0

自从我刚刚创建分支以来,最后一条命令按预期提供了零次提交。之前的命令为我提供了我在开发分支上的实际提交数量减去合并提交的实际数量

无法正常工作:

git checkout current-development-branch
git rev-list --no-merges --count HEAD
361
git checkout -b testbranch_1
git rev-list --no-merges --count HEAD
361

在这两种情况下,我都获得了开发分支和主分支中所有提交(间接)从中提交的提交数。


1

如果您使用的是UNIX系统,则可以

git log|grep "Author"|wc -l

-2

你也可以做git log | grep提交| wc -l

并返回结果


1
这是不可靠的。例如,它将匹配两次在提交消息中具有“ commit”的提交。
rdb

@rdb不,不会。它只会输出包含单词“ commit” 的行数,因此一行将永远不会被计算两次。
iBug

@iBug:您错过了重点。如果提交消息中包含单词“ commit”,则它将与git log输出中的“ commit a1b2c ...”行分开显示,因此提交将在结果中计入两次。更糟的是,如果提交消息在两条单独的行上两次包含单词“ commit”,则该提交消息将被删除。
rdb
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.