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

后端 未结 2 1913
时光取名叫无心
时光取名叫无心 2021-02-01 00:58

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 e

2条回答
  •  温柔的废话
    2021-02-01 01:34

    The native solution:

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

    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 need 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 --  | 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 --  | tail -1
    

提交回复
热议问题