How do I check the date and time of the latest `gi

2020-02-02 06:47发布

How do I check the date and time of the latest git pull that was executed? I frequently need to know when the code changed on a server when something goes wrong.

标签: git git-pull
10条回答
叼着烟拽天下
2楼-- · 2020-02-02 07:02
stat -c %Y .git/FETCH_HEAD

Will give you a unix timestamp of the last modification of that file. Git writes the FETCH_HEAD file every time you pull or fetch, even if there was nothing to pull.

查看更多
女痞
3楼-- · 2020-02-02 07:02

Use python: python -c "import os;print os.stat('.git/FETCH_HEAD').st_mtime"

查看更多
趁早两清
4楼-- · 2020-02-02 07:03

Cross Platform (OSX/Linux) Bash Solution

Heavily inspired by @smooves answer: https://stackoverflow.com/a/9229377/622276 and comments.

But I am maintaining my own bash prompt git integration

With the source here: https://github.com/neozenith/dotfiles/blob/master/bash-scripts/function_parse_git_prompt.sh

msys version in Git Bash for Windows works identical to the linux version.

I'm compiling the cross platform options into a case statement. So it will fork a fetch process on any git repo I navigate into that is older than fifteen minutes since last fetch so the rest of my prompt script knows if I have stuff to pull.

Git radar used to to this but it required saving a file with timestamp of when the last fetch was called. This writes no temporary files.

git rev-parse --show-toplevel just means if I'm anywhere in a git repo it will get the repo root so we can reference the .git folder path.

# 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
查看更多
叛逆
5楼-- · 2020-02-02 07:06
$ # 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]
查看更多
▲ chillily
6楼-- · 2020-02-02 07:09
python -c "import os, datetime ;print datetime.datetime.fromtimestamp(os.stat('.git/FETCH_HEAD').st_mtime)"

or

python3 -c "import os, datetime ;print(datetime.datetime.fromtimestamp(os.stat('.git/FETCH_HEAD').st_mtime))"
查看更多
乱世女痞
7楼-- · 2020-02-02 07:10

On a hunch, I tried "stat -c %y .git/FETCH_HEAD", and got a human-readable printout of the time:

> stat -c %y .git/FETCH_HEAD
2015-02-24 17:42:08.072094410 -0500

Furthermore, you can add when = !stat -c %y .git/FETCH_HEAD to the [alias] section in your ~/.gitconfig file (it's safest to do this automatically by running the following command line in any git repo)

git config --global alias.when '!stat -c %y .git/FETCH_HEAD'

and then you are able to find this info with your new "command", anytime:

> git when
2015-02-23 15:07:53.086254218 -0500

[Then it occurred to me to do "man stat", and I found that there are a bunch of other % parameters available for the 'stat' program. YMMV.]

查看更多
登录 后发表回答