19

From what I can see, Git commit dates and author dates are only accurate to one second. I'm wondering if this is as precise as they get or can I get the timestamp in milliseconds or even microseconds.

This command returns the UNIX Timestamp using the commit hash of the first commit:

git show -s --format="%ct" 2d9afdfb9e2fa9b349312734b462bb7d57a684ee

Result: 1421437899

What is GIT's commit-date or author-date timestamp precision?

Jack Vial
  • 2,354
  • 1
  • 28
  • 30
  • 2
    It's just unix timestamp and as precise as timestamp is. Actually git commit is specially formatted text with times stored as timestamps – Alexey Ten Jan 30 '15 at 14:09

2 Answers2

25

The resolution of Git commit/author dates is 1 second, which, as pointed out by Alexey Ten and Edward Thomson, is also the resolution of Unix timestamps.

An interesting experiment that you can conduct is to

  • create a commit, and
  • amend it very quickly, without changing anything (not even the commit message).

As you may know, amending a commit actually creates a new commit. Typically, the new commit would have a different timestamp and, therefore, a different commit ID from that of the first commit. However, you can write a script that creates the commit and amends it within the same system-clock second (with a bit of luck!), thereby producing a commit whose hash is the same as the first commit's.

First, set things up:

$ mkdir testGit
$ cd testGit
$ git init

Then write this to a script file (called commitAmend.sh below)

#!/bin/sh

# create content and commit
printf "Hello World.\n" > README.md
git add README.md
git commit -m "add README"
git log

# amend the commit
git commit --amend --no-edit
git log

and run it:

$ sh commitAmend.sh
[master (root-commit) 11e59c4] add README
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
commit 11e59c47ba2f9754eaf3eb7693a33c22651d57c7
Author: jub0bs <xxxxxxxxxxx>
Date:   Fri Jan 30 14:25:58 2015 +0000

    add README
[master 11e59c4] add README
 Date: Fri Jan 30 14:25:58 2015 +0000
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
commit 11e59c47ba2f9754eaf3eb7693a33c22651d57c7
Author: jub0bs <xxxxxxxxxxx>
Date:   Fri Jan 30 14:25:58 2015 +0000

add README

Same timestamp, same hash!

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 4
    Interesting experiment. +1 – VonC Jan 30 '15 at 14:32
  • Thanks for the detailed answer. So is there a way to get the time when the file was added to Git using `git add`? – Jack Vial Jan 30 '15 at 15:11
  • 1
    @Jack No. Staging a file does not record any associated timestamp. What happens when you stage a file is explained in http://stackoverflow.com/questions/25351450/what-does-adding-to-the-index-really-mean-in-git/25352119#25352119 – jub0bs Jan 30 '15 at 15:14
  • @Jubobs Great explanation of `git add` in that link. I think the UNIX timestamp of the first commit might be good enough for what I'm trying to do. I have a database migration script that is triggered by a post-merge hook and I want all my .sql migration files to run in the correct order but if multiple files are added to the migration directory and committed at the same time they all have the same timestamp which is a problem because I'm mapping the timestamps to the keys with the file paths as the values of an assoc array then sorting on the keys. – Jack Vial Jan 30 '15 at 16:18
  • @Jack Might be worth asking for help in another question, but I won't be able to answer that one, I'm afraid. – jub0bs Jan 30 '15 at 16:20
6

It is one second; while git-show is prettifying the output a bit, you can see the raw commit information using the git-cat-file command. For example:

% git cat-file commit HEAD
tree 340c0a26a5efed1f029fe1d719dd2f3beebdb910
parent 1ac5acdc695b837a921897a9d42acc75649cfd4f
author Edward Thomson <ethomson@edwardthomson.com> 1422564055 -0600
committer Edward Thomson <ethomson@edwardthomson.com> 1422564055 -0600

My witty comment goes here.

You can see indeed that this is a Unix timestamp with a resolution of 1 second.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187