Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git - calculate how many lines of code were added/changed by someone
# Run this in the project repo from the command-line
# http://stackoverflow.com/a/4593065/99923
git log --shortstat --author "Xeoncross" --since "2 weeks ago" --until "1 week ago" | grep "files changed" | awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "files changed", files, "lines inserted:", inserted, "lines deleted:", deleted}'
@felixbuenemann

This comment has been minimized.

Copy link

@felixbuenemann felixbuenemann commented Sep 7, 2018

You can replace grep "files changed" | awk '{… with awk '/files changed/ {..., that way one less process is needed.

@sastorsl

This comment has been minimized.

Copy link

@sastorsl sastorsl commented Jun 3, 2019

Thanks a lot for this!
Expanded on your idea and came up with this to count across projects:

for i in */.git
do
    (cd ${i%/*}
    git log --shortstat --author "Xeoncross" | awk '
        BEGIN { f=0 ; i=0 ; d=0 }
        /files? changed/ {f+=$1; i+=$4; d+=$6}
        END { printf("files changed: %4i - lines inserted: %6i lines deleted: %6i - %s\n", f, i, d, p) }' p=${i%/*}
    )
done | awk '
    { f+=$3 ; i+=$7 ; d+=$10 ; print }
    END { print "----" ; printf("files changed: %4i - lines inserted: %6i lines deleted: %6i - %s\n", f, i, d, "Total") }'

Also updated "files changed" to "files? changed" which will match both "file changed" and "files changed".

@jamiehaywood

This comment has been minimized.

Copy link

@jamiehaywood jamiehaywood commented Dec 6, 2019

Just another improvement on this:

author="YourAuthorHere"
for i in */.git
do
    (cd ${i%/*}
    git log --shortstat --author $author | awk '
        BEGIN { f=0 ; i=0 ; d=0 }
        /files? changed/ {f+=$1; i+=$4; d+=$6}
        END { printf("files changed: %4i - lines inserted: %6i lines deleted: %6i - %s\n", f, i, d, p) }' p=${i%/*}
    )
done | awk '
    { f+=$3 ; i+=$7 ; d+=$10 ; print }
    END { print "----" ; printf("files changed: %4i - lines inserted: %6i lines deleted: %6i - %s\n", f, i, d, "Total") }'
@SheldonWangRJT

This comment has been minimized.

Copy link

@SheldonWangRJT SheldonWangRJT commented May 10, 2020

or just make a function and put into bash profile

function gline() {
	cd path/to/your/repo
	git log --shortstat --author $1 --since "10 years ago" --until "1 week ago" | grep "files changed" | awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "files changed", files, "lines inserted:", inserted, "lines deleted:", deleted}'
}

and then you can do
gline YourName

@kirhgoff

This comment has been minimized.

Copy link

@kirhgoff kirhgoff commented Jun 17, 2020

It misses commits where only 1 file was changed:
1 file changed, 3 insertions(+), 2 deletions(-)

could modify grep part as
egrep "file[s]* changed"

@lack3r

This comment has been minimized.

Copy link

@lack3r lack3r commented Sep 11, 2020

Very good try. However, the command does not produce correct results.
First, it can misinterpret deletions as additions.
Please consider the following stat:
2 files changed, 2 deletions(-)
In case a commit does not have any additions, but only removals, as the one shown above, the number of lines removed will be added to the total of the lines added.
Given the example provided above, the command will report that 2 lines were added, and 0 were deleted, while in fact, it was the opposite.
Second, it misses the commits where 1 file was changed as mentioned by @kirhgoff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment