Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 803
Seth Johnson Avatar asked Mar 05 '10 21:03

Seth Johnson


People also ask

How do you check when was a file created?

You can show the "creation date" that is stored for files in the Windows Explorer easily: Switch Windows Explorer to column view, right click a column header and and in the context menu that pops up select "Creation Date" for enabling the additional column. Note that this setting is folder-specific.

How do you check the history of a file in Git?

Git file History provides information about the commit history associated with a file. To use it: Go to your project's Repository > Files. In the upper right corner, select History.

Do git commits store timestamps?

Conclusion. The only timestamps that are stored in a Git repository are the "author" and "committer" dates in the commit objects. The tree and blob objects do not contain any timestamps. There is no timestamp information about the files in the local filesystem contained in the Git repository.

Which command is used to see the history of a git repository?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.


2 Answers

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

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

like image 161
shingara Avatar answered Sep 22 '22 23:09

shingara


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 
like image 39
ruvim Avatar answered Sep 21 '22 23:09

ruvim