计算 Git 分支上的提交次数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11657295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count the number of commits on a Git branch
提问by aaronbauman
I found this answer already: Number of commits on branch in gitbut that assumes that the branch was created from master.
我已经找到了这个答案:Number of commits on branch in git但假设分支是从 master 创建的。
How can I count the number of commits along a branch withoutrelying on that assumption?
如何在不依赖该假设的情况下计算分支上的提交次数?
In SVN this is trivial, but for some reason is really difficult to figure out in git.
在 SVN 中,这是微不足道的,但由于某种原因,在 git 中真的很难弄清楚。
回答by Peter van der Does
To count the commits for the branch you are on:
要计算您所在分支的提交:
git rev-list --count HEAD
for a branch
对于一个分支
git rev-list --count <branch-name>
If you want to count the commits on a branch that are made since you created the branch
如果要计算自创建分支以来分支上的提交
git rev-list --count HEAD ^<branch-name>
This will count all commits ever made that are not on the branch-name as well.
这将计算所有不在分支名称上的提交。
Examples
例子
git checkout master
git checkout -b test
<We do 3 commits>
git rev-list --count HEAD ^master
Result: 3
结果:3
If your branch comes of a branch called develop
:
如果您的分支来自一个名为 的分支develop
:
git checkout develop
git checkout -b test
<We do 3 commits>
git rev-list --count HEAD ^develop
Result: 3
结果:3
Ignoring Merges
忽略合并
If you merge another branch into the current branch without fast forward and you do the above, the merge is also counted. This is because for git a merge is a commit.
如果您在没有快进的情况下将另一个分支合并到当前分支中并且您执行上述操作,则合并也被计算在内。这是因为对于 git 来说,合并是一次提交。
If you don't want to count these commits add --no-merges
:
如果您不想计算这些提交,请添加--no-merges
:
git rev-list --no-merges --count HEAD ^develop
回答by Asnad Atta
To see total no of commits you can do as Peter suggested above
要查看提交的总数,您可以按照 Peter 上面的建议进行操作
git rev-list --count HEAD
And if you want to see number of commits made by each person try this line
如果您想查看每个人的提交次数,请尝试此行
git shortlog -s -n
will generate output like this
将产生这样的输出
135 Tom Preston-Werner
15 Hyman Danger Canty
10 Chris Van Pelt
7 Mark Reid
6 remi
回答by scanny
It might require a relatively recent version of Git, but this works well for me:
它可能需要一个相对较新的 Git 版本,但这对我来说效果很好:
git rev-list --count develop..HEAD
This gives me an exact count of commits in the current branch having its base on master.
这为我提供了基于 master 的当前分支中提交的准确计数。
The command in Peter's answer, git rev-list --count HEAD ^develop
includes many more commits, 678 vs 97 on my current project.
彼得回答中的命令git rev-list --count HEAD ^develop
包括更多提交,我当前项目中的 678 与 97。
My commit history is linear on this branch, so YMMV, but it gives me the exact answer I wanted, which is "How many commits have I added so far on this feature branch?".
我的提交历史在这个分支上是线性的,所以 YMMV,但它给了我我想要的确切答案,即“到目前为止我在这个功能分支上添加了多少提交?”。
回答by kyb
How much commits was done to current branch since begin of history, not counting commits from merged branches:
自历史开始以来对当前分支进行了多少提交,不计算来自合并分支的提交:
git rev-list HEAD --count --first-parent
From documentation git rev-list --help:
从文档git rev-list --help:
--first-parent
Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch, because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual commits brought in to your history by such a merge. Cannot be combined with --bisect.
--first-parent
看到合并提交后,只关注第一个父提交。在查看特定主题分支的演变时,此选项可以提供更好的概览,因为合并到主题分支往往只是为了不时调整上游更新,而此选项允许您忽略引入的单个提交你的历史通过这样的合并。不能与 --bisect 结合使用。
Note:Shallow clone will shrink the history size. E.g. if you clone with --depth 1
, will return 1.
注意:浅克隆将缩小历史记录大小。例如,如果您使用 进行克隆--depth 1
,将返回 1。
number of commits done sincesome other commit:
自其他提交以来完成的提交数:
git rev-list HEAD abc0923f --count --first-parent
or the same:
或相同:
git rev-list abc0923f.. --count --first-parent
or use any other git reference:
或使用任何其他git 参考:
git rev-list master tag-v20 --count --first-parent
Count commits done since 2018 year
计算自 2018 年以来完成的提交
git rev-list HEAD --count --first-parent --since=2018-01-01
01-01-2018, 01.01.2018, 2018.01.01 also works.
01-01-2018、01.01.2018、2018.01.01 也有效。
git rev-label
git rev-label
I wrote a script to get version-revision from Git in format like '$refname-c$count-g$short$_dirty'
which expands to master-c137-gabd32ef
.
Help is included to script itself.
我编写了一个脚本来从 Git 以'$refname-c$count-g$short$_dirty'
扩展为master-c137-gabd32ef
.
帮助包含在脚本本身中。
回答by Remear
How about git log --pretty=oneline | wc -l
怎么样 git log --pretty=oneline | wc -l
That should count all the commits from the perspective of your current branch.
从当前分支的角度来看,这应该计算所有提交。
回答by inorganik
I like doing git shortlog -s -n --all
. Gives you a "leaderboard" style list of names and number of commits.
我喜欢做git shortlog -s -n --all
。为您提供名称和提交数量的“排行榜”样式列表。
回答by kjw0188
One way to do it is list the log for your branch and count the lines.
一种方法是列出分支的日志并计算行数。
git log <branch_name> --oneline | wc -l
回答by Paul Lan
Well, the selected answer doesn't work if you forked your branch out of unspecific branch (i.e., not master
or develop
).
好吧,如果您将分支从非特定分支(即 notmaster
或develop
)分叉出来,则所选答案将不起作用。
Here I offer a another way I am using in my pre-push
git hooks.
在这里,我提供了我在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,.*/\(.*\),,')
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
For more analysis, please visit my blog
更多分析请访问我的博客
回答by sdoe
As the OP references Number of commits on branch in gitI want to add that the given answers there also work with any other branch, at least since git version 2.17.1 (and seemingly more reliably than the answer by Peter van der Does):
由于 OP 引用Number of commits on branch in git我想补充一点,那里给出的答案也适用于任何其他分支,至少从 git 版本 2.17.1 开始(并且似乎比 Peter van der 的答案更可靠):
working correctly:
正常工作:
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
The last command gives zero commits as expected since I just created the branch. The command before gives me the real number of commits on my development-branch minus the merge-commit(s)
由于我刚刚创建了分支,最后一个命令按预期提供了零提交。之前的命令给了我开发分支上的实际提交数减去合并提交
not working correctly:
工作不正常:
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
In both cases I get the number of all commits in the development branch and master from which the branches (indirectly) descend.
在这两种情况下,我都获得了分支(间接)下降的开发分支和主分支中所有提交的数量。
回答by james
You can also do git log | grep commit | wc -l
你也可以做 git log | grep 提交 | wc -l
and get the result back
并取回结果