有没有简单的方法来计算Git中两次提交之间更改的行数?

我知道我可以做一个git diff,并数行,但这似乎很乏味。我还想知道如何做到这一点,在行计数中只包括我自己的提交。


当前回答

如果你想检查两个分支或提交之间插入、删除和提交的数量。

使用提交id:

git log <commit-id>..<commit-id> --numstat --pretty="%H" --author="<author-name>" | awk 'NF==3 {added+=$1; deleted+=$2} NF==1 {commit++} END {printf("total lines added: +%d\ntotal lines deleted: -%d\ntotal commits: %d\n", added, deleted, commit)}'

使用分支:

git log <parent-branch>..<child-branch> --numstat --pretty="%H" --author="<author-name>" | awk 'NF==3 {added+=$1; deleted+=$2} NF==1 {commit++} END {printf("total lines added: +%d\ntotal lines deleted: -%d\ntotal commits: %d\n", added, deleted, commit)}'

其他回答

该命令将比较本地文件和远程文件

git diff --stat

如果你想检查两个分支或提交之间插入、删除和提交的数量。

使用提交id:

git log <commit-id>..<commit-id> --numstat --pretty="%H" --author="<author-name>" | awk 'NF==3 {added+=$1; deleted+=$2} NF==1 {commit++} END {printf("total lines added: +%d\ntotal lines deleted: -%d\ntotal commits: %d\n", added, deleted, commit)}'

使用分支:

git log <parent-branch>..<child-branch> --numstat --pretty="%H" --author="<author-name>" | awk 'NF==3 {added+=$1; deleted+=$2} NF==1 {commit++} END {printf("total lines added: +%d\ntotal lines deleted: -%d\ntotal commits: %d\n", added, deleted, commit)}'

关于上次提交的简短统计信息:

git diff --shortstat HEAD~1 HEAD

在我的例子中,这给了我以下信息:

 254 files changed, 37 insertions(+), 10773 deletions(-)

插入和删除是受影响的行。

虽然以上答案都是正确的,但如果你需要计算最后提交的次数,下面的答案是方便的

下面一个是最近5次提交的计数

Git diff $(Git log -5——pretty=format:"%h" | tail -1)——shortstat

获取最近10次提交的计数

Git diff $(Git log -10——pretty=format:"%h" | tail -1)——shortstat

generic -用你需要的最后提交数改变N

git diff $(git log -N——pretty=format:"%h" | tail -1)——shortstat

获取自启动以来所有提交的计数

Git diff $(Git log——pretty=format:"%h" | tail -1)——shortstat

对于懒惰的人,使用git log——stat。