Is there any easy way to calculate the number of lines changed between two commits in Git?
I know I can do a git diff
, and count the lines, but this seems tedious. I'd also like to know how I can do this, including only my own commits in the line counts.
This question is related to
git
Though all above answers are correct, below one is handy to use if you need count of last many commits
below one is to get count of last 5 commits
git diff $(git log -5 --pretty=format:"%h" | tail -1) --shortstat
to get count of last 10 commits
git diff $(git log -10 --pretty=format:"%h" | tail -1) --shortstat
generic - change N with count of last many commits you need
git diff $(git log -N --pretty=format:"%h" | tail -1) --shortstat
to get count of all commits since start
git diff $(git log --pretty=format:"%h" | tail -1) --shortstat
If you want to check the number of insertions, deletions & commits, between two branches or commits.
using commit id's:
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)}'
using branches:
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 commit1 commit2
EDIT: You have to specify the commits as well (without parameters it compares the working directory against the index). E.g.
git diff --stat HEAD^ HEAD
to compare the parent of HEAD
with HEAD
.
Assuming that you want to compare all of your commits between abcd123 (the first commit) and wxyz789 (the last commit), inclusive:
git log wxyz789^..abcd123 --oneline --shortstat --author="Mike Surname"
This gives succinct output like:
abcd123 Made things better
3 files changed, 14 insertions(+), 159 deletions(-)
wxyz789 Made things more betterer
26 files changed, 53 insertions(+), 58 deletions(-)
For the lazy, use git log --stat
.
git diff --shortstat
gives you just the number of lines changed and added. This only works with unstaged changes. To compare against a branch:
git diff --shortstat some-branch
git log --numstat just gives you only the numbers
Another way to get all change log in a specified period of time
git log --author="Tri Nguyen" --oneline --shortstat --before="2017-03-20" --after="2017-03-10"
Output:
2637cc736 Revert changed code
1 file changed, 5 insertions(+), 5 deletions(-)
ba8d29402 Fix review
2 files changed, 4 insertions(+), 11 deletions(-)
With a long output content, you can export to file for more readable
git log --author="Tri Nguyen" --oneline --shortstat --before="2017-03-20" --after="2017-03-10" > /mnt/MyChangeLog.txt
I just solved this problem for myself, so I'll share what I came up with. Here's the end result:
> git summary --since=yesterday
total: 114 file changes, 13800 insertions(+) 638 deletions(-)
The underlying command looks like this:
git log --numstat --format="" "$@" | awk '{files += 1}{ins += $1}{del += $2} END{print "total: "files" files, "ins" insertions(+) "del" deletions(-)"}'
Note the $@
in the log command to pass on your arguments such as --author="Brian"
or --since=yesterday
.
Escaping the awk to put it into a git alias was messy, so instead, I put it into an executable script on my path (~/bin/git-stat-sum
), then used the script in the alias in my .gitconfig
:
[alias]
summary = !git-stat-sum \"$@\"
And it works really well. One last thing to note is that file changes
is the number of changes to files, not the number of unique files changed. That's what I was looking for, but it may not be what you expect.
Here's another example or two
git summary --author=brian
git summary master..dev
# combine them as you like
git summary --author=brian master..dev
git summary --all
Really, you should be able to replace any git log
command with git summary
.
If you want to see the changes including the # of lines that changed between your branch and another branch,
git diff the_other_branch_name --stat
Source: Stackoverflow.com