Christian Goetze Christian Goetze - 1 year ago 68
Git Question

How can I determine when a git ref was created?

This is not the question you think it is.

I want to know when a specific ref was created. I do not want to know what the author timestamp or the committer timestamp is.

For example: if I pull from a remote repo, which updates my local repo, I want to retrieve the timestamps of the time the local repo refs were created.

[remote-host]$ git show --format='%H %cD' refs/heads/master
25f2508a379adf2f8aa264afe6f7e5046943274a Mon, 1 Aug 2016 11:33:12 -0700

[remote-host]$ logout
Connection to remote-host closed.
[local-host]$ git fetch
remote: Counting objects: 165073, done
remote: Finding sources: 100% (1836/1836)
remote: Total 1836 (delta 484), reused 1374 (delta 484)
Receiving objects: 100% (1836/1836), 4.10 MiB | 0 bytes/s, done.
Resolving deltas: 100% (484/484), completed with 262 local objects.
[local-host]$ git show --format='%H %cD' 25f2508a379adf2f8aa264afe6f7e5046943274a
25f2508a379adf2f8aa264afe6f7e5046943274a Mon, 1 Aug 2016 11:33:12 -0700

[local-host]$ date
Mon Aug 1 11:50:44 PDT 2016

The output I want is
Mon Aug 1 11:50:44 PDT 2016
(i.e. the time of the fetch), not the committer date which is correctly identical on both the remote and the local host.

I thought of using
git reflog
, but the output of that command is strangely void of any timestamp info.

Answer Source

I don't think git tracks the information you're looking for, which is I think more accurately described as "the date a blob was created on my local system".

You could:

  • First look for a loose blob with a matching content hash, and if you find it, assume the mtime of that blob is the time at which is was created on your system (since it shouldn't get modified by any subsequent operation):

    if test -f .git/objects/${cid:0:2}/${cid:2}; then
      ls -l .git/objects/${cid:0:2}/${cid:2}
  • If you don't find a loose object, look through your pack files to find the corresponding content id, and if you find it, assume the mtime of the corresponding packfile is the date you want:

    for idx in .git/objects/pack/*.idx; do
      if git show-index < $idx | grep $cid; then
        ls -l ${idx/idx/pack}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download