git 如何从git存储库获取上次提交日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25563455/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:23:48  来源:igfitidea点击:

How do I get last commit date from git repository?

gitgit-commit

提问by Narendra Vadnere

I need the last commit date in git. This means the latest update date in my program.

我需要 git 中的最后提交日期。这意味着我的程序中的最新更新日期。

I used the command : $ git log -1but this command will give me the date from the local repository. Rather I need date from remote repository.

我使用了命令 : $git log -1但这个命令会给我来自本地存储库的日期。相反,我需要来自远程存储库的日期。

I tried some commands as follow.

我尝试了一些命令如下。

git log -n 1 origin/Sprint-6.
git rev-parse --verify HEAD

回答by love

The following command will be helpful:

以下命令会有所帮助:

git log -1 --format=%cd 

This will print the latest change date for one file. The -1 shows one log entry (the most recent), and --format=%cdshows the commit date. See the documentation for git-logfor a full description of the options.

这将打印一个文件的最新更改日期。-1 显示一个日志条目(最近的),并--format=%cd显示提交日期。有关选项的完整说明,请参阅git-log的文档。

回答by Eric Leschinski

Get the last commit date:

获取最后提交日期:

You want the "repository wide last commit date for a given git user and git project, for a given branch.

对于给定的分支,您需要“给定 git 用户和 git 项目的存储库范围的最后提交日期。

The date you're after is the latest date shown when you visit your repo and go to commits -> masterfor example:

你所追求的日期是你访问你的仓库时显示的最晚日期commits -> master,例如:

https://github.com/sentientmachine/TeslaAverageGainByMonthWeekDay/commits/master

https://github.com/sentientmachine/TeslaAverageGainByMonthWeekDay/commits/master

The top of the page shows the latest commit date.

页面顶部显示最新提交日期。

Get the last local commit date in git using terminal

使用终端获取 git 中的最后一个本地提交日期

Use git help logfor more info on format codes to pass to --formatto tell git log what kind of data to fetch.

使用git help log对格式代码的详细信息传递给--format告诉git的日志什么样的数据的获取。

The last commit date in git:

git 中的最后提交日期:

git log -1 --format="%at" | xargs -I{} date -d @{} +%Y/%m/%d_%H:%M:%S
#prints 2018/07/18 07:40:52

But as you pointed out, you have to run that command on the machine that performed that last commit. If the last commit date was performed on another machine, the above command only reports local last commit... So:

但是正如您所指出的,您必须在执行最后一次提交的机器上运行该命令。如果最后一次提交日期是在另一台机器上执行的,上面的命令只报告本地最后一次提交......所以:

Or Repository wide: Get the last git commit date

或存储库范围:获取上次 git 提交日期

Same as above, but do a git pull first.

与上面相同,但先执行 git pull。

git pull; 
git log -1 --format="%at" | xargs -I{} date -d @{} +%Y/%m/%d_%H:%M:%S
#prints 2018/07/18 09:15:10

Or use git's JSON API:

或者使用 git 的 JSON API:

git pulls on a schedule aren't cool because it's slow and you're banging the server with unnecessary network traffic. Just query the git rest api:

按计划执行 git pull 并不酷,因为它很慢,而且您正在用不必要的网络流量冲击服务器。只需查询 git rest api:

#assuming you're using github and your project URL is visible to public:
# https://github.com/yourusername/your_repo_name

#then do:
curl https://api.github.com/repos/yourusername/your_repo_name/commits/master

That blasts you in the face with a screen full of json, so send it your favorite json parser and get the field called date:

这会让你看到一个充满 json 的屏幕,所以将它发送到你最喜欢的 json 解析器并获取名为的字段date

curl https://api.github.com/repos/<your_name>/<your_repo>/commits/master 2>&1 | \
grep '"date"' | tail -n 1
#prints "date": "2019-06-05T14:38:19Z"

From comments below, gedgehas handy dandy improvements to incantations:

从下面的评论中,gedge对咒语进行了方便的改进:

git log -1 --date=format:"%Y/%m/%d %T" --format="%ad"
2019/11/13 15:25:44

Or even simpler: ( https://git-scm.com/docs/git-log/1.8.0)

或者更简单:(https://git-scm.com/docs/git-log/1.8.0

git --no-pager log -1 --format="%ai"
2019-12-13 09:08:38 -0500

Your choices are north, south, east, and "Dennis".

您的选择是北、南、东和“丹尼斯”。