Finding the date/time a file was first added to a Git repository

Is there a simple Git command to determine the "creation date" of a file in a repository, i.e. the date it was first added?

It would be best if it can determine this even through file renames/moves. I'd like it to be a computer-readable one-line output; it may be that I haven't figured out the correct git log <fname> options to do this.


Solution 1:

git log --format=%aD <FILE> | tail -1

With this command you can out all date about this file and extract the last

Solution 2:

The native solution:

git log --diff-filter=A --follow --format=%aD -1 -- <fname> 

It gives the last "creation date" of a file in a repository, and does it regardless of file renames/moves.

-1 is synonym to --max-count=1 and it limits the number of commits to output (to be not more than one in our case).

This limit is needed since a file can be added more than once. For example, it can be added, then removed, then added again. In such case --diff-filter=A will produce several lines for this file.

To get the first creation date in the first line we should use --reverse option without limitation (since limit is applied before ordering).

git log --diff-filter=A --follow --format=%aI --reverse -- <fname> | head -1

%aI gives author date in the strict ISO 8601 format (e.g. 2009-06-03T07:08:51-07:00).

But this command doesn't work properly due to the known bug in Git (see "--follow is ignored when used with --reverse" conversation in git maillist). So, we are forced to use some work around for awhile to get the first creation date. E.g.:

git log --diff-filter=A --follow --format=%aI -- <fname> | tail -1