241

Other than parsing git log for the date string, is there a Git native way to report the date of a certain commit?

Samer Buna
  • 8,021
  • 9
  • 37
  • 55

6 Answers6

352

The show command may be what you want. Try

git show -s --format=%ci <commit>

Other formats for the date string are available as well. Check the manual page for details.

srobinson
  • 346
  • 2
  • 4
  • 17
Tim Henigan
  • 57,640
  • 11
  • 83
  • 76
  • 23
    To get the commit without its diff, use `log -1` instead of `show`. – Josh Lee Sep 28 '10 at 16:42
  • 41
    Or add the '-s' to the command: `git show -s --format="%ci" ` – aprock Sep 28 '10 at 17:38
  • 10
    For future users: you can view the *author date* with `%ai`. – user541686 Jan 12 '15 at 02:27
  • @Mehrdad I'm looking to accomplish this with all the files in a `repo git ls-tree -r --name-only HEAD` sent through a loop doesn't give me a specific date-time. – DBS Apr 21 '16 at 22:02
  • 11
    to get just unix timestamp: use git show -s --format=%ct – xiaoweiz Oct 20 '16 at 13:37
  • 5
    If you want another date format, you can use `git show -s --format=%cd --date=short ` (will give e.g. 2016-11-02) or `git show -s --format=%cd --date=short ` or `git show -s --format=%cd --date=format:%Y ` (this example will print only the year) For details see [this answer](http://stackoverflow.com/a/19742762/935676). – amoebe Nov 02 '16 at 15:53
  • 1
    It is _significantly_ faster to use `log -1` rather than `show -s` for large merge commits so I definitely recommend using `log -1` if you are trying to find stale branches. This sped it up from hours to minutes in the case of the monorepo I'm working with right now. – marczych Jul 25 '18 at 01:54
  • `git show -s --format=%ci` is what I wanted (for most recent commit) – Ryan Feb 06 '20 at 17:20
33

If you want to see only the date of a tag you'd do:

git show -s --format=%ci <mytagname>^{commit}

which gives: 2013-11-06 13:22:37 +0100

Or do:

git show -s --format=%ct <mytagname>^{commit}

which gives UNIX timestamp: 1383740557

Marcus Philip
  • 450
  • 4
  • 6
  • This seems to give the date of the commit a tag points to, not the date of the tag its self. – hoijui Oct 22 '21 at 06:20
25

If you like to have the timestamp without the timezone but local timezone do

git log -1 --format=%cd --date=local

Which gives this depending on your location

Mon Sep 28 12:07:37 2015
schoetbi
  • 10,981
  • 10
  • 50
  • 71
5

You can use the git show command.

To get the last commit date from git repository in a long(Unix epoch timestamp):

  • Command: git show -s --format=%ct
  • Result: 1605103148

Note: You can visit the git-show documentation to get a more detailed description of the options.

Keshav Lodhi
  • 1,820
  • 1
  • 14
  • 19
5

In case that you want to format the date (or hour) by yourself:

git show -s --date=format:'%Y%m%d-%H%M' --format=%cd  <commit id | default is the last commit>

# example output:
20210712-1948
dsaydon
  • 3,797
  • 4
  • 44
  • 49
0

if you got troubles with windows cmd command and .bat just escape percents like that

git show -s --format=%%ct

The % character has a special meaning for command line parameters and FOR parameters. To treat a percent as a regular character, double it: %%

Syntax : Escape Characters, Delimiters and Quotes