Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a commit count for each file in a git repository

Tags:

I'm looking for a way to see statistics about how often each file in a git repository has been changed. Basically how often a file has been committed and was actually different from the previous version.

Additionally, is there a way to get the date when a file was last changed?

I'm a git novice and haven't found anything about this. Any help would be greatly appreciated!

like image 908
Phendan Avatar asked Aug 17 '18 19:08

Phendan


1 Answers

Here's two approaches.

You can use git ls-files to get a list of all files.

You can use git log --oneline <file> to get a list of all commits which affected that file, each on one line. Then pipe it to wc -l to get a count.

Put them together with your favorite programming language.

This is useful if you only want to look at a few files.


You can use git log --pretty=format: --name-only to get a list of all files changed in each commit. Commits are separated by newlines. For example...

$ git log --pretty=format: --name-only
benchmark/README.md

benchmark/README.md
benchmark/app_erb.rb
benchmark/app_erb.yml
benchmark/erb_render.rb
benchmark/erb_render.yml

benchmark/README.md

benchmark/README.md

This shows benchmark/README.md changed 4 times and the rest changed once.

Parse it with your favorite programming language.

This is more efficient if you want to scan the whole history.


Additionally, is there a way to get the date when a file was last changed?

Yes, git log --pretty=format:... allows you to customize what is shown. %ad shows the author date (or %cd for commit date, author date and commit dateare potentially different). --date allows you to change the date format. git log --date=iso --pretty=format:%ad <filename> gives you all the times a file was changed in ISO 8601 format. Add --author-date-order to ensure they're ordered by author date. Then add a -1 to get just the last change.

$ git log -1 --author-date-order --date=iso --pretty=format:%ad somefile
2018-08-16 03:12:27 +0800

You can put this together with --name-only to get a complete listing of all commits, what files they changed, and when.

$ git log --author-date-order --date=iso --pretty=format:%ad --name-only
2018-07-10 16:03:51 +0000
benchmark/README.md

2018-07-10 15:58:52 +0000
benchmark/README.md
benchmark/app_erb.rb
benchmark/app_erb.yml
benchmark/erb_render.rb
benchmark/erb_render.yml

2018-07-10 15:51:29 +0000
benchmark/README.md

2018-07-10 15:49:42 +0000
benchmark/README.md
like image 165
Schwern Avatar answered Oct 11 '22 10:10

Schwern