我已经找到了这个答案:git中分支上的提交数, 但假定分支是从master创建的。
在不依赖该假设的情况下,如何计算分支中的提交次数?
在SVN中,这是微不足道的,但是由于某些原因,在git中确实很难解决。
我已经找到了这个答案:git中分支上的提交数, 但假定分支是从master创建的。
在不依赖该假设的情况下,如何计算分支中的提交次数?
在SVN中,这是微不足道的,但是由于某些原因,在git中确实很难解决。
Answers:
要计算分支的提交,您需要:
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
git log比其他建议效果更好。
要查看未提交的总数,您可以按照彼得在上面的建议进行操作
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的当前分支中的提交的准确计数。
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
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之间-清单。
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
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。
脚本本身包含帮助。
好吧,如果您将分支从非特定分支分支出来(即,不是master或develop),则所选答案将不起作用。
在这里,我提供了另一种在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
有关更多分析,请访问我的博客
由于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
在这两种情况下,我都获得了开发分支和主分支中所有提交(间接)从中提交的提交数。