How can I see the date multiple files were created on git?
I want to see the date of git creation (date of first commit where they were added) of all the files on a specified directory.
I'll break my solution into steps.
Get a list of all files in the repository
$ git ls-files
This returns a list of relative paths of all files in the repository.
Get the SHA-1 of the first commit of a given file:
$ git rev-list HEAD <file> | tail -n 1
This will return a list of all parentless commits for a given file, in reverse chronological order. The last one is the SHA-1 hash of the first commit for the given file.
You can verify this by running git log --raw <hash>. You should see something like:
commit <commit_hash> Author: Susy Q <firstname.lastname@example.org> Date: Wed Aug 24 12:36:34 2011 -0400 Add new module 'example.py' :000000 100644 0000000... <hash>... A example.py
Show the date of a given commit
$ git show -s --format="%ci" <hash>
Bringing it all together in a bash script:
#!/bin/bash for file in $(git ls-files) do HASH=$(git rev-list HEAD "$file" | tail -n 1) DATE=$(git show -s --format="%ci" $HASH --) printf "%-35s %s\n %s\n" "$file" $HASH: "$DATE" done
Well, i devised a solution which is not elegant but sort of works:
for file in dir/*; do echo -e "$file: \t\t `git log $file|grep Date|tail -1`"; done
it works by running git log on each file and then greping it to get only the dates of each commit regarding that file. Then tail -1 ensures that i will only get the date of the first commit. The echo -e "..." is there just so that it prints the info in a (not-so)friendly way!