Jörn M. Horschig Jörn M. Horschig - 4 months ago 7
Git Question

git: list all file names and the hashes of their latest commit

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
...

Answer

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