Skip to content

Instantly share code, notes, and snippets.

@fieldse
Last active April 19, 2024 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 Dec 19, 2019

Credit to Timothy Keith for the Git date function. Sorting and columns added.

@datYori
Copy link

datYori commented Apr 16, 2024

In my case this was more accurate

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

@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