Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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
...
like image 232
Dr.J Avatar asked Aug 05 '16 11:08

Dr.J


1 Answers

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
like image 165
gavv Avatar answered Oct 02 '22 15:10

gavv