Teknowledgist
Teknowledgist

Reputation: 403

GitHub: How to show commits for all files in folder

It's not hard to find information on how to see all the files in a particular commit, but I would like the inverse. IOW, I want a list of all files in a folder and the commit that put them there. Something like:

File1.txt   c7b1fd463f40b39d7f199ee650b58a8ba153b64a
File2.md    d9e3ca571d40b38d7fab90cf50b48a82c1d3a32f
File3.c     3207cab3b49da7be5d9a7ef8594bef1935e15fd1
...

Is there some way to do that?

Upvotes: 3

Views: 1428

Answers (1)

Bajal
Bajal

Reputation: 5986

Assuming that you are interested to know what is the most recent commit on your files, this command will give that for a given file: git log -n 1 -- <your_filename>. To produce output like you wish, you can use this snippet:

for file in *; do 
  log=$(git log -n 1 --pretty=format:%H -- $file)
  echo -e "$file\t$log"
done

..which is simply looping through the files in current folder and executing the git command, extracting just the SHA.

If you are interested in the first commit that put the file there instead of the last commit that touched the file, replace the git command with git log --diff-filter=A --pretty=format:%H (see link)

If you are doing this repeatedly you can put the snippet in a shell script and add to your $PATH.

Warning: It might take a while to run if there are too many files. You can change the file pattern to restrict it if possible.

Upvotes: 2

Related Questions