get age of git commit using hash

705 Views Asked by At

If we have a git commit hash:

42f35295744738750a665dc846400c8040659d03

is there a way to determine/calculate the date it was created without any more info?

1

There are 1 best solutions below

5
On

Not exactly, no. You can easily extract either of the two time-stamps (author-date and committer-date) but neither one necessarily has anything to do with the actual creation date. For instance:

$ GIT_AUTHOR_DATE='1970-1-1 00:00:00' GIT_COMMITTER_DATE='1970-1-1 00:00:00' git commit -m gotcha
[master (root-commit) 30e9626] gotcha
 1 file changed, 1 insertion(+)
 create mode 100644 foo
$ git log --pretty=fuller | sed 's/@/ /'
commit 30e9626dce78fd41c7ef6fae317e0ed61863f0cd
Author:     Chris Torek <chris.torek gmail.com>
AuthorDate: Thu Jan 1 00:00:00 1970 -0800
Commit:     Chris Torek <chris.torek gmail.com>
CommitDate: Thu Jan 1 00:00:00 1970 -0800

    gotcha

But, if you are not worried about the computer clock being wildly wrong and/or users deliberately faking their time-stamps, use %at or %ct or any of the variants of those to extract the author or committer time stamps:

$ git log --no-walk --format=%at HEAD
28800
$ git log --no-walk --format=%cd HEAD
Thu Jan 1 00:00:00 1970 -0800

See the PRETTY FORMATS section of the git log documentation for details.