83

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

106

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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.
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!
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 Using --reverse / head gives different - and incorrect - results for some files. tail just works.
What is the difference between author date & comitter date (%aD vs %ct)?
|
56

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 Comments

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, 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).
@AaronMahan It turns out that it is a bug in Git and it was already reported. The answer is updated accordingly.
git has a --no-pager options, so you can leave off the | tail -1 (which also makes it easier to work with xargs)
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.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.