【发布时间】:2011-02-28 22:48:28
【问题描述】:
如何查看最新执行的git pull 的日期和时间?当出现问题时,我经常需要知道服务器上的代码何时更改。
【问题讨论】:
如何查看最新执行的git pull 的日期和时间?当出现问题时,我经常需要知道服务器上的代码何时更改。
【问题讨论】:
这是一个小的 git 包装器。使用名称git 和权限chmod a+x git 安装它。然后将last_successful_fetch添加到.git/info/exclude。
当您想查看结果时,请使用stat last_successful_fetch。
#!/bin/sh
# This script just invokes git as expected, plus one additional action:
# If there stands last_successful_fetch in .git/info/exclude, then
# this file will be touched in top dir.
"`which --all git | uniq | head -n 2 | tail -n 1`" "$@"
status=$?
if [ _"$1" = _pull -o _"$1" = _fetch ]; then
if grep last_successful_fetch "`git rev-parse --git-dir`/info/exclude" >/dev/null 2>&1; then
[ $status = 0 ] && touch last_successful_fetch
fi
fi
【讨论】:
深受@smooves 答案:https://stackoverflow.com/a/9229377/622276 和 cmets 的启发。
但我正在维护自己的bash prompt git integration
这里有源: https://github.com/neozenith/dotfiles/blob/master/bash-scripts/function_parse_git_prompt.sh
Windows 版 Git Bash 中的msys 版本与 linux 版本的工作方式相同。
我正在将跨平台选项编译成一个案例语句。因此,它将在我导航到的任何 git repo 上分叉一个提取过程,该过程自上次提取以来超过 15 分钟,因此我的提示脚本的其余部分知道我是否有东西要提取。
Git radar 习惯于这样做,但它需要保存一个文件,其中包含调用最后一次提取时的时间戳。这不会写入临时文件。
git rev-parse --show-toplevel 仅表示如果我在 git repo 中的任何位置,它将获取 repo 根目录,因此我们可以引用 .git 文件夹路径。
# No repo == no more work
local REPO_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
if [[ -n $REPO_ROOT && -e "$REPO_ROOT/.git/FETCH_HEAD" ]]; then
case $OSTYPE in
darwin*)
local LAST_FETCH="$(stat -f '%m' $REPO_ROOT/.git/FETCH_HEAD)"
local FETCH_THRESHOLD="$(date -v-15m +%s)"
;;
*)
local LAST_FETCH="$(stat -c %Y $REPO_ROOT/.git/FETCH_HEAD)"
local FETCH_THRESHOLD="$(date -d'15 minutes ago' +%s)"
;;
esac
# Fork fetch process in background
if [[ $LAST_FETCH -lt $FETCH_THRESHOLD ]]; then
git fetch --all --quiet --prune 2> /dev/null &
fi
fi
【讨论】:
git show -1 --stat
此 git 命令显示最新更改提交时间和日期以及消息
【讨论】:
python -c "import os, datetime ;print datetime.datetime.fromtimestamp(os.stat('.git/FETCH_HEAD').st_mtime)"
或
python3 -c "import os, datetime ;print(datetime.datetime.fromtimestamp(os.stat('.git/FETCH_HEAD').st_mtime))"
【讨论】:
正如用户:https://stackoverflow.com/users/83646/smoove 所建议的那样,您可以通过检查 .git/FETCH_HEAD 的修改时间戳来找到上次在 repo 上调用 git pull 的时间:git 每次拉取或抓取,即使没有什么可以拉取。
示例: {master} vinegupt@bhling69(/imsgit_local/work/vinegupt/ims_18.5a/ims_common)$ stat -c %y .git/FETCH_HEAD
2018-02-12 02:01:50.487160386 +0530
【讨论】:
stat -f %Sm .git/FETCH_HEAD 但似乎对我来说效果很好。
$ # for the latest pull even if there's nothing new
$ stat -c %y .git/FETCH_HEAD
2017-12-15 11:24:25.000000000 +0100
$
$ # for records of updated references
$ git reflog --date=iso
db2bba84 (HEAD -> master, origin/master, origin/HEAD) HEAD@{2017-12-14 11:28:39 +0100}: pull: Fast-forward
37fe73ad HEAD@{2017-12-03 17:09:32 +0100}: pull: Fast-forward
c4107fcd HEAD@{2017-11-27 18:53:40 +0100}: clone: from https://github.com/macports/macports-base
$
$ # for a more detailed view of the latter
$ git log -g
commit db2bba84d5e8cd82ec94a19129deb91ef62287bb (HEAD -> master, origin/master, origin/HEAD)
Reflog: HEAD@{0} (me <me@machine.local>)
Reflog message: pull: Fast-forward
Author: Ryan Schmidt <ryandesign@macports.org>
Date: Wed Dec 13 10:23:47 2017 -0600
portutil.tcl: Fix renames that supply the -force option
Treat $options as a list not as a string.
See: https://trac.macports.org/ticket/55492
[snip]
【讨论】:
凭直觉,我尝试了“stat -c %y .git/FETCH_HEAD”,并得到了一个人类可读的时间打印输出:
> stat -c %y .git/FETCH_HEAD
2015-02-24 17:42:08.072094410 -0500
此外,您还可以添加
when = !stat -c %y .git/FETCH_HEAD 到 ~/.gitconfig 文件中的 [alias] 部分(通过在任何 git repo 中运行以下命令行自动执行此操作是最安全的)
git config --global alias.when '!stat -c %y .git/FETCH_HEAD'
然后您可以随时使用新的“命令”找到此信息:
> git when
2015-02-23 15:07:53.086254218 -0500
[然后我想到做“man stat”,我发现“stat”程序还有很多其他可用的 % 参数。 YMMV。]
【讨论】:
-c 不是 stat 的有效选项。
使用python:python -c "import os;print os.stat('.git/FETCH_HEAD').st_mtime"
【讨论】:
stat -c %Y .git/FETCH_HEAD
将为您提供该文件最后一次修改的 unix 时间戳。 每次拉取或获取时,Git 都会写入 FETCH_HEAD 文件,即使没有可拉取的内容。
【讨论】:
stat -f '%m' .git/FETCH_HEAD
stat -f '%Sm' $(git rev-parse --show-toplevel)/.git/FETCH_HEAD
.git/refs/heads/master,当git pull 导致来自远程master 分支的更改时,其时间戳会更改,但git pull 报告时时间戳不会更改没有变化。
在非裸存储库中(裸存储库对git pull 没有意义),git 将所有对分支提示的更改和当前分支的想法记录在 .git/logs 的“reflogs”中。您可以使用git log -g 查看这些内容。
但是,尽管日志文件确实有时间戳,但 git log -g 似乎不会打印它。但是,例如,如果您查看.git/logs/HEAD,您会发现该格式非常易于解析 - 它包含 ref(或 HEAD)从什么更改、更改为、谁更改、何时以及活动消息。
【讨论】:
git show 命令显示最近提交的日期。这不是提交被拉到本地存储库的日期,但 Git 不会保留此类拉取信息。
您可以使用服务器上文件的 ctime(创建时间)找到上次拉取的时间。例如:
ls -lct
显示每个文件的 ctime,按最近的顺序排列。
【讨论】:
git show 显示了当前分支的提示提交日期,它不一定是 repo 中最近的提交,更不用说最新的 fetch/pull 日期。我还建议检查 smoove 的答案。
git show 告诉你本地克隆在哪个提交。 (我知道这是旧的,但我最终通过搜索来到这里。)