计算Git分支上的提交数

计算Git分支上的提交数,git,Git,我已经找到了这个答案: 但这假设分支是从master创建的 如何在不依赖该假设的情况下计算分支上的提交数 在SVN中,这很简单,但由于某些原因,在git中很难理解。git log--pretty=oneline | wc-l 这应该从当前分支的角度计算所有提交。一种方法是列出分支的日志并计算行数 git log <branch_name> --oneline | wc -l git日志--oneline | wc-l 要计算您所在分支的提交次数: git rev-list --co

我已经找到了这个答案: 但这假设分支是从master创建的

如何在不依赖该假设的情况下计算分支上的提交数

在SVN中,这很简单,但由于某些原因,在git中很难理解。

git log--pretty=oneline | wc-l


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

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

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

要计算您所在分支的提交次数:

git rev-list --count HEAD
分公司

git rev-list --count <branch-name>
结果:3

忽略合并 如果在不使用快进的情况下将另一个分支合并到当前分支中,并且执行了上述操作,则合并也将被计算在内。这是因为对于git来说,合并就是提交

如果不想计算这些提交的次数,请添加
--无合并

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

要查看提交的总数,您可以按照Peter的建议执行

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

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

git rev-list --count develop..HEAD
这为我提供了当前分支中基于master的提交的精确计数

彼得的回答中的命令,
git rev list--count HEAD^develop
包括更多的提交,在我当前的项目中是678对97

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

您也可以这样做 git日志| grep提交| wc-l


然后返回结果

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

如果您将分支机构从非特定分支机构(即,非
master
develope
)中分支出来,则所选答案不起作用

    git shortlog -s -n | awk '/Author/ { print $1 }'
这里我提供了另一种在我的
预推
git钩子中使用的方法

# 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
#推送前运行生产构建
echo“[INFO]run.git/hooks/pre-push”
echo“[INFO]检查是否只有一次提交”
#file.git/hooks/pre-push
currentBranch=$(git符号参考头| sed-e,.*/\(.*\),\1')
gitLog=$(git log--graph--abbrev commit--decoration--first parent HEAD)
commitCountOfCurrentBranch=0
startCountCommit=“”
baseBranch=“”
而read-r行;做
#如果git日志行以“*提交aaface7(源/分支机构名称)”或“提交ae4f131(头->分支机构名称)”之类的内容开头
#这意味着它在我们的分行名称上
MatchedCommitString=“$([$line=~\*[:space:]]提交[[:space:].\(.*)]]]和&echo${BASH\u重新匹配[1]}”
如果[[!-z${matchedCommitSubstring}]];然后
如果[[$line=~$currentBranch]];然后
startCountCommit=“true”
其他的
startCountCommit=“”
如果[[-z${baseBranch}]];然后
baseBranch=$([${matchedcommitsbstring}=~(.*)\,]]和&echo${BASH\u REMATCH[1]}| | echo${matchedcommitsbstring})
fi
fi
fi
如果[[!-z${startCountCommit}&&$line=~^\*[:space:][commit[:space:][];然后
((commitCountOfCurrentBranch++)
fi
done自历史开始以来,对当前分支进行了多少提交,不包括合并分支的提交:
来自文档git版本列表--帮助:

——第一个家长

在看到合并提交时,仅遵循第一个父提交。在查看特定主题分支的演变时,此选项可以提供更好的概述,因为合并到主题分支往往只是调整以适应不时更新的上游,并且此选项允许您忽略通过这种合并带来的历史记录中的单个提交。不能与--bisect组合

注意:浅克隆将缩小历史记录大小。例如,如果使用
--深度1
进行克隆,将返回1

自某个其他提交之后完成的提交次数 或相同:

git rev-list abc0923f.. --count --first-parent
或使用任何其他:


统计自2018年以来完成的承诺 2018年1月1日、2018年1月1日、2018年1月1日也有效


我编写了一个脚本,以类似于
“$refname-c$count-g$short$\u dirty”
的格式从Git获取版本修订,它扩展到
master-c137-gabd32ef

脚本本身包含帮助。

作为OP参考,我想补充的是,这里给出的答案也适用于任何其他分支,至少从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
由于我刚刚创建了分支,最后一个命令按预期提供零提交。前面的命令给出了开发分支上的实际提交数减去合并提交数

工作不正常:

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

在这两种情况下,我都会得到开发分支和主分支(间接)下降的所有提交的数量。

如果您使用的是UNIX系统,您可以这样做

git log|grep "Author"|wc -l

您可以使用这个命令,该命令在gitbash/unix上使用awk来获取提交次数

    git shortlog -s -n | awk '/Author/ { print $1 }'

你算哪一列?这是第一个吗?没有一个显示正确的数字,例如master和branchname显示相同数量的提交;我真的不允许使用代码,但这应该表明它确实有效git init====$touch test.txt====$git add.====$git提交-a===$git版本列表--计数头=>1====$git版本列表--计数主控=>1=====$git签出-b测试=====$git版本列表--计数测试=>1=====$git版本列表--计数头^master=>0======$touch test2.txt=========$git addgit提交-a===$git rev list--count master=>1=====$git rev list--count test=>2====$git rev list--count HEAD^master=>1====我同意@bot。这些不是很准确。例如,尝试添加一些合并委员会
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
git log|grep "Author"|wc -l
    git shortlog -s -n | awk '/Author/ { print $1 }'