77

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.

2 Answers 2

100

git log --follow --format=%ad --date default <FILE> | tail -1

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

The option %ad shows the date in the format specified by the --date setting, one of relative, local, iso, iso-strict, rfc, short, raw, human, unix, format:<strftime-string>, default.

10
  • 15
    You'll also need --follow to follow the log through renames as the OP asked. Also good practice to add -- before the filename just in case it clashes with an option or ref name. Mar 6, 2010 at 1:07
  • Since I was planning on calling git from Python, I was hoping for a command that doesn't require piping. I suppose I can just get the final line with Python, though. Any thoughts? Thanks! Mar 7, 2010 at 21:00
  • 8
    It is better to use reverse in this solution, i.e. git log --follow --format=%aD --reverse -- <fname> | head -1 (so, you don't need to read the all lines in Python, just the first one)
    – ruvim
    Oct 12, 2014 at 23:12
  • 1
    @ruvim Using --reverse / head gives different - and incorrect - results for some files. tail just works.
    – user711807
    Oct 17, 2016 at 19:36
  • 1
    What is the difference between author date & comitter date (%aD vs %ct)? Jul 13, 2017 at 16:02
55

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
10
  • 1
    If you need to inhibit the pager, add -c pager.log=false: git -c pager.log=false log --diff-filter=A --follow --format=%aD -1 -- <fname>.
    – mjs
    Oct 5, 2016 at 13:05
  • 1
    @mjs, yes, a file can be added more than once. For example, it can be added, then be removed, then be added again (I updated the answer).
    – ruvim
    Oct 13, 2016 at 16:46
  • 1
    @AaronMahan It turns out that it is a bug in Git and it was already reported. The answer is updated accordingly.
    – ruvim
    Oct 30, 2016 at 12:18
  • 1
    git has a --no-pager options, so you can leave off the | tail -1 (which also makes it easier to work with xargs) Oct 17, 2018 at 4:26
  • 1
    I'm extracting creation date for 30k files, this takes 5 hours. Halfway through I encountered 2 files, where --diff-filter=A --follow would yield no output. This seems to be files where the latest commit had a merge conflict.
    – neoneye
    Apr 14, 2021 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.