如何在 Git 中获取一堆文件的最后提交日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8611486/
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
How to get the last commit date for a bunch of files in Git?
提问by jonderry
What's the simplest one-liner to get the last commit date for a bunch of files in a Git repository (i.e., for each file, the date of the last commit that changed that file)?
获取 Git 存储库中一堆文件的最后提交日期的最简单的单行代码是什么(即,对于每个文件,更改该文件的最后一次提交的日期)是什么?
The context for this is that I'm using other Bash commands to find a long list of files matching some criteria, and I'd like to just pipe this list into a Git command to get the last commit date for each file.
这样做的上下文是,我正在使用其他 Bash 命令来查找与某些条件匹配的一长串文件,我想将此列表通过管道传输到 Git 命令中以获取每个文件的最后提交日期。
回答by Greg Hewgill
The following command will be helpful:
以下命令会有所帮助:
git log -1 --format=%cd filename.txt
This will print the latest change date for one file. The -1
shows one log entry (the most recent), and --format=%cd
shows the commit date. See the documentation for git-log
for a full description of the options.
这将打印一个文件的最新更改日期。该-1
节目一个日志条目(最新的),并--format=%cd
显示了提交日期。有关git-log
选项的完整说明,请参阅文档。
You should be able to easily extend this for a group of files.
您应该能够轻松地为一组文件扩展它。
回答by nickf
Slightly extending Greg's answer, git log
can take multiple paths in its argument. It will then show only the commits which included those paths. Therefore, to get the latest commit for a group of files:
稍微扩展 Greg 的答案,git log
可以在其论点中采用多种路径。然后它将只显示包含这些路径的提交。因此,要获取一组文件的最新提交:
git log -1 --format=%cd -- fileA.txt fileB.txt fileC.txt
I'm pretty rubbish at shell scripting, so I'm not exactly sure how to construct that command via piping, but maybe that'd be a good topic for another question.
我在 shell 脚本方面很垃圾,所以我不确定如何通过管道构造该命令,但也许这会是另一个问题的好话题。
回答by diemo
Here's a one liner using find (broken into several for readability, but thanks to the trailing backslashes, copy–paste should work):
这是一个使用 find 的单行(为了可读性分成几个,但由于尾随反斜杠,复制粘贴应该可以工作):
find <dirs...> -name '<pattern>' <any other predicate to get what you want> \
-exec git log -1 --format="AAA%ai NNN" '{}' \; \
-exec echo '{}' XXX \; \
| tr \n N | sed -e 's/AAA/\n/g' | sed -e 's/NNNN/ /g' -e 's/XXX.*//g'
The overly complex newline mangling with tr and sed is just there to get date and filename on one line, and to ignore untracked files. You have to make sure that none of your files contain those silly markers AAA XXX NNNN.
使用 tr 和 sed 进行过于复杂的换行修改只是为了在一行中获取日期和文件名,并忽略未跟踪的文件。您必须确保没有任何文件包含那些愚蠢的标记 AAA XXX NNNN。
回答by Penghe Geng
Use git ls-files
to find git files, and then git log
to format the output. But since git log
will group several file together when they share same commit time, I prefer to have it process one file at a time and then sort the result.
使用git ls-files
发现git的文件,然后git log
格式化输出。但是由于git log
在共享相同提交时间时会将多个文件组合在一起,因此我更喜欢一次处理一个文件,然后对结果进行排序。
The resulted one-liner:
结果单行:
for f in $(git ls-files); do git --no-pager log --color -1 --date=short --pretty=format:'%C(cyan)%ai%Creset' -- $f ; echo " $f"; done |sort -r
If you want to add it to your .bashrc:
如果你想把它添加到你的 .bashrc 中:
function gls() {
for f in $(git ls-files); do git --no-pager log --color -1 --date=short --pretty=format:'%C(cyan)%ai%Creset' -- $f ; echo " $f"; done |sort -r
}
Then running gls
will output something like:
然后运行gls
将输出如下内容:
2019-09-30 11:42:40 -0400 a.c
2019-08-20 11:59:56 -0400 b.conf
2019-08-19 16:18:00 -0400 c.c
2019-08-19 16:17:51 -0400 d.pc
The result is in time descending order.
结果按时间降序排列。