11

I would like to get a list of all files in my branch, preferably in a tree view, together with the hash of the commit they have last been modified in (i.e. not the hash of the file itself but of the commit). Is there a neat git-command to do this, or do I really have to crawl through the log?

This question is related to How do I find the most recent git commit that modified a file? but I want to get a list of all files, for example:

6f88a51 abc.h
3f5d6fb abc.cpp
3f5d6fb bcd.h
1964be2 bcd.cpp
...
Community
  • 1
  • 1
Dr.J
  • 334
  • 3
  • 12

3 Answers3

10

Command:

$ git ls-files -z \ | GIT_PAGER= xargs -0 -L1 -I'{}' git log -n 1 --format="%h {}" -- '{}' f5fe765 LICENSE 0bb88a1 README.md 1db10f7 example/echo.go e4e5af6 example/echo_test.go ...

Notes:

  • git ls-files lists all files added to git recursively (unlike find, it excludes untracked files and .git)
  • xargs -L1 executes given command for every input argument (filename)
  • xargs -I{} enables substitution of {} symbol with input argument (filename)
  • using git ls-files -z and xargs -0 changes delimiter from \n to \0, to avoid potential problems with white-spaces in filenames
  • clearing GIT_PAGER prevents git log from piping it's output to less
gavv
  • 4,649
  • 1
  • 23
  • 40
3
for i in $(find -type f | grep -v '.git'); 
    do echo -n "$i - "; 
    git log --pretty="format:%h" -1 $i | cat; 
    echo; 
done

That should do the trick, on bash

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
blue112
  • 52,634
  • 3
  • 45
  • 54
  • thanks, also gives me what I want, but I liked gavvs solution better as it's more elegant – Dr.J Aug 05 '16 at 14:51
0

You can simply use the ls-tree command

git ls-tree HEAD

This will show you the latest files with their hashes.

enter image description here

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 2
    This is incorrect. It shows the SHA-1 of the blobs and trees (which OP explicitly stated he does not want) instead of the SHA-1 of the latest commit which touched the files. – Alderath Aug 05 '16 at 12:34
  • @Alderath is right, it shows file hashes, not commit hashes. Also, it doesn't print directories recursively. – gavv Aug 05 '16 at 12:50
  • 2
    Though this is not what's expected, git ls-tree -r HEAD could show all the blobs in the HEAD's tree. – ElpieKay Aug 05 '16 at 13:14
  • this is exactly what I did not want, but thanks for thinking along ;) – Dr.J Aug 05 '16 at 14:50
  • Help op or not, this post is useful for my use case, so thanks! – akostadinov Sep 30 '21 at 11:58