28

How can I list files sorted by their modification time?

I want the git modification time, not the system modification time. For example, if I have a (committed) file README, then

touch README

will change the system modification time... but the git status would remain unchanged.

If I try

git ls-files -z | xargs -0 ls  -t

this will sort by the system modification time.

Is there any option to git ls-files that would list files sorted by their git modification time?

Vijay Murthy
  • 841
  • 2
  • 9
  • 19

5 Answers5

32

Is there any option to git ls-files that would list files sorted by their git modification time?

I don't think so. One possible way would be to iterate over the files, get the timestamp using git log, and sort the output.

The following might work for you:

while read file; do echo $(git log --pretty=format:%ad -n 1 --date=raw -- $file) $file; done < <(git ls-tree -r --name-only HEAD) | sort -k1,1n
troelskn
  • 115,121
  • 27
  • 131
  • 155
devnull
  • 118,548
  • 33
  • 236
  • 227
14

Best answer is by Mikko Rantalainen on serverfault:

git ls-tree -r --name-only HEAD -z | TZ=UTC xargs -0n1 -I_ git --no-pager log -1 --date=iso-local --format="%ad _" -- _ | sort 
Jonathan Ben-Avraham
  • 4,615
  • 2
  • 34
  • 37
  • This is best because it (a) shows human-readable timestamps, unlike devnull 's answer above and (b) shows the timestamps in general, and also more than just the most recent, unlike datinc 's and thill 's answers above. – ijoseph Feb 19 '21 at 18:52
  • 1
    To support cases where files are renamed in git, use this for the xargs bit: `xargs -0n1 -I_ sh -c 'git --no-pager log --follow --date=iso-local --format="%ad _" -- _ | tail -1'` – Connor Clark Apr 19 '23 at 20:43
7

Easiest and fastest is probably

git diff-index      --name-only --diff-filter=A @
git log --pretty='' --name-only | awk '!seen[$0]++'

git diff-index ... lists all your newly-tracked files first, then git log ... lists all files in history most-recently-touched first (| awk ... removes duplicates). It'll still list historical files that have been deleted in the current checkout, but it hardly seems worth the complexity to weed those out.

rob74
  • 4,939
  • 29
  • 31
jthill
  • 55,082
  • 5
  • 77
  • 137
6

I was able to sort files in a folder by using the following command

git ls-files -z -- "${dir}" | xargs -0 -n1 -I{} -- git log -1 --format="%at {}" {} | sort | tail -n1 | cut -d " " -f2-
datinc
  • 3,404
  • 3
  • 24
  • 33
  • 1
    I needed to sort by creation time, so modified it like this and I think it worked: `git ls-files -z -- "${dir}" | xargs -0 -n1 -I{} -- sh -c 'git log -1 --format="%at {}" {} | tail -1' | sort | cut -d " " -f2-` – haridsv Dec 11 '19 at 07:39
  • Running that command on Mac, I get `fatal: empty string is not a valid pathspec. please use . instead if you meant to match all paths` – Matt Fletcher May 13 '21 at 10:20
3

This won't show ALL files, but it does give you a quick idea on what changed in the order your looking for:

git log --name-only
Elletlar
  • 3,136
  • 7
  • 32
  • 38