How to retrieve the last modification date of all files in a Git repository
A simple answer would be to iterate through each file and display its modification time, i.e.:
git ls-tree -r --name-only HEAD | while read filename; do
echo "$(git log -1 --format="%ad" -- $filename) $filename"
done
This will yield output like so:
Fri Dec 23 19:01:01 2011 +0000 Config
Fri Dec 23 19:01:01 2011 +0000 Makefile
Obviously, you can control this since its just a bash script at this point--so feel free to customize to your heart's content!
This approach also works with filenames that contain spaces:
git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {}
Example output:
2015-11-03 10:51:16 -0500 .gitignore
2016-03-30 11:50:05 -0400 .htaccess
2015-02-18 12:20:26 -0500 .travis.yml
2016-04-29 09:19:24 +0800 2016-01-13-Atlanta.md
2016-04-29 09:29:10 +0800 2016-03-03-Elmherst.md
2016-04-29 09:41:20 +0800 2016-03-03-Milford.md
2016-04-29 08:15:19 +0800 2016-03-06-Clayton.md
2016-04-29 01:20:01 +0800 2016-03-14-Richmond.md
2016-04-29 09:49:06 +0800 3/8/2016-Clayton.md
2015-08-26 16:19:56 -0400 404.htm
2016-03-31 11:54:19 -0400 _algorithms/acls-bradycardia-algorithm.htm
2015-12-23 17:03:51 -0500 _algorithms/acls-pulseless-arrest-algorithm-asystole.htm
2016-04-11 15:00:42 -0400 _algorithms/acls-pulseless-arrest-algorithm-pea.htm
2016-03-31 11:54:19 -0400 _algorithms/acls-secondary-survey.htm
2016-03-31 11:54:19 -0400 _algorithms/acls-suspected-stroke-algorithm.htm
2016-03-31 11:54:19 -0400 _algorithms/acls-tachycardia-algorithm-stable.htm
...
The output can be sorted by modification timestamp by adding | sort
to the end:
git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort
This is a small tweak of Andrew M.'s answer. (I was unable to comment on his answer.)
Wrap the first $filename in double quotes, in order to support filenames with embedded spaces.
git ls-tree -r --name-only HEAD | while read filename; do
echo "$(git log -1 --format="%ad" -- "$filename") $filename"
done
Sample output:
Tue Jun 21 11:38:43 2016 -0600 subdir/this is a filename with spaces.txt
I appreciate that Andrew's solution (based on ls-tree) works with bare repositories! (This isn't true of solutions using ls-files.)