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

后端 未结 2 1914
时光取名叫无心
时光取名叫无心 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:28

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

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

    0 讨论(0)
  • 2021-02-01 01:34

    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 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 -- <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
    
    0 讨论(0)
提交回复
热议问题