Skip to content

Instantly share code, notes, and snippets.

@fieldse
Last active June 4, 2024 17:43
Show Gist options
  • Save fieldse/84aabca169b2a14899fc05e89fded298 to your computer and use it in GitHub Desktop.
Save fieldse/84aabca169b2a14899fc05e89fded298 to your computer and use it in GitHub Desktop.
Git tricks: file creation dates for all files in a Git repo directory

Git - print file creation dates

Scenario:

You have a directory of files in a git repository and you want to extract their original creation dates. Git does not track file creation date metadata, but you can get the first time the file was added in a commit.

This one-liner will print the dates and filenames sorted, in column format.

Solution:

# One-liner
WIDTH=35 ; for filename in * ; do created_date=$(git log --follow --format=%ad --date default $filename | tail -1); printf "%-${WIDTH}s |      %s \n" "$created_date" $filename ; done | sort


# More readable multi-line

WIDTH=35        # Column padding width
DIR=.           # You can substitute this for a directory name
for filename in "${DIR}"/*
do 
  created_date="$(git log --follow --format=%ad --date default $filename | tail -1)"
  printf "%-${WIDTH}s |      %s \n" "$created_date" $filename
done | sort

credit for enhancements to @datYori

credit for original inspiration to Timothy Keith - https://keithieopia.com/post/2017-03-09-git-file-creation-date/.

@fieldse
Copy link
Author

fieldse commented Apr 19, 2024

Thanks for the enhancement @datYori !

I'll update the original with a credit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment