我已经找到了这个答案:Number of commits on branch in git 但是假设分支是从master创建的。
如何在没有依赖该假设的情况下计算分支中的提交数量?
在SVN中,这是微不足道的,但由于某些原因,很难在git中找到答案。
答案 0 :(得分:249)
计算您所在分支的提交:
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来说,merge是一个提交。
如果您不想计算这些提交,请添加--no-merges
:
git rev-list --no-merges --count HEAD ^develop
答案 1 :(得分:47)
这一行
git shortlog -s -n
将生成这样的输出
135 Tom Preston-Werner
15 Jack Danger Canty
10 Chris Van Pelt
7 Mark Reid
6 remi
答案 2 :(得分:31)
它可能需要一个相对较新版本的Git,但这对我很有用:
git rev-list --count develop..HEAD
这给了我一个基于master的当前分支的确切提交数。
彼得的回答中的命令,git rev-list --count HEAD ^develop
包括更多的提交,在我当前的项目中为678对97。
我的提交历史在这个分支上是线性的,所以YMMV,但它给了我想要的确切答案,这是&#34;到目前为止我在这个功能分支上添加了多少次提交?&#34;。< / p>
答案 3 :(得分:5)
git rev-list HEAD --count --first-parent
来自文档 git rev-list --help :
--first-parent
在看到合并提交时,仅遵循第一个父提交。在查看特定主题分支的演变时,此选项可以提供更好的概述,因为合并到主题分支往往只是关于不时调整到更新的上游,并且此选项允许您忽略引入的单个提交通过这样的合并你的历史。不能与--bisect结合使用。
git rev-list HEAD abc0923f --count --first-parent
或相同:
git rev-list abc0923f.. --count --first-parent
或使用任何其他git reference:
git rev-list master tag-v20 --count --first-parent
注意:浅层克隆会缩小历史记录大小。例如。如果您使用--depth 1
进行克隆,则会返回1.
git rev-list HEAD --count --first-parent --since=2018-01-01
01-01-2018,01.01.2018,2018.01.01也有效。
git rev-label
我编写了一个脚本,以'$refname-c$count-g$short$_dirty'
之类的格式从Git获取版本修订版,扩展为master-c137-gabd32ef
。
脚本本身包含帮助。
cd ~/bin
wget 'https://gitlab.com/kyb/build-info-header/raw/master/git-revision.sh?inline=false' -qO git-revision.sh && chmod +x git-revision.sh
ln -s $PWD/git-revision.sh /usr/local/bin/git-revision
git revision
git revision '$refname-c$count-g$short$_dirty'
答案 4 :(得分:2)
git log --pretty=oneline | wc -l
这应该从当前分支的角度来计算所有提交。
答案 5 :(得分:2)
我喜欢git shortlog -s -n --all
。为您提供名称和提交数量的“排行榜”样式列表。
答案 6 :(得分:1)
一种方法是列出分支的日志并计算行数。
git log <branch_name> --oneline | wc -l
答案 7 :(得分:1)
好吧,如果您将分支从非特定分支分叉(即,不是master
或develop
),则所选答案不起作用。
在这里,我提供了另一种我在pre-push
git hooks中使用的方式。
# 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
如需更多分析,请访问my blog
答案 8 :(得分:1)
如果您使用的是UNIX系统,则可以这样做
git log|grep "Author"|wc -l
答案 9 :(得分:0)
OP引用Number of commits on branch in 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
在这两种情况下,我都获得了开发分支和主分支中所有提交的数量,这些分支(间接地)从中下降。
答案 10 :(得分:0)
您可以使用此命令,该命令在git bash / unix上使用awk来获取提交次数。
git shortlog -s -n | awk '/Author/ { print $1 }'
答案 11 :(得分:-1)
你也可以这样做 git log | grep commit | wc -l </ p>
并获得结果