查找文件首次添加到 Git 存储库的日期/时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2390199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding the date/time a file was first added to a Git repository
提问by Seth Johnson
Is there a simple Gitcommand to determine the "creation date" of a file in a repository, i.e. the date it was first added?
是否有一个简单的Git命令来确定存储库中文件的“创建日期”,即它第一次添加的日期?
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.
最好是即使通过文件重命名/移动也能确定这一点。我希望它是一个计算机可读的单行输出;可能是我还没有想出正确的git log <fname>
选择来做到这一点。
回答by shingara
git log --format=%aD <FILE> | tail -1
git log --format=%aD <FILE> | tail -1
With this command you can out all date about this file and extract the last
使用此命令,您可以输出有关此文件的所有日期并提取最后一个
回答by ruvim
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).
-1
是同义词--max-count=1
,它限制提交到输出的数量(在我们的例子中不超过一次)。
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.
由于可以多次添加文件,因此需要此限制。例如,可以先添加,然后删除,然后再添加。在这种情况下,--diff-filter=A
将为此文件生成几行。
To get the firstcreation date in the first line we should use --reverse
option without limitation (since limitis applied before ordering).
要获得第一行中的第一个创建日期,我们应该使用--reverse
option without limit(因为在订购之前应用了限制)。
git log --diff-filter=A --follow --format=%aI --reverse -- <fname> | head -1
%aI
givesauthor date in the strict ISO 8601format (e.g. 2009-06-03T07:08:51-07:00
).
%aI
以严格的ISO 8601格式(例如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 firstcreation date. E.g.:
但是由于 Git 中的已知错误,此命令无法正常工作(请参阅“ --follow 与 --reverse对话在 git maillist 中与 --reverse 一起使用时被忽略”)。因此,我们被迫暂时使用一些解决方法来获得第一个创建日期。例如:
git log --diff-filter=A --follow --format=%aI -- <fname> | tail -1