Git commit date
Asked Answered
M

7

287

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

Marigolde answered 28/9, 2010 at 16:24 Comment(1)
Good stuff : git-scm.com/docs/pretty-formatsKisser
C
411

The show command may be what you want. Try

git show --no-patch --format=%ci <commit>

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

Capetian answered 28/9, 2010 at 16:36 Comment(9)
To get the commit without its diff, use log -1 instead of show.Oraliaoralie
Or add the '-s' to the command: git show -s --format="%ci" <commit>Paphos
For future users: you can view the author date with %ai.Resect
@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.Maund
to get just unix timestamp: use git show -s --format=%ct <commit>Guidon
If you want another date format, you can use git show -s --format=%cd --date=short <commit> (will give e.g. 2016-11-02) or git show -s --format=%cd --date=short <commit> or git show -s --format=%cd --date=format:%Y <commit> (this example will print only the year) For details see this answer.Ecklund
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.Michal
git show -s --format=%ci is what I wanted (for most recent commit)Allelomorph
The git show -s variant will sometimes output some extra information beyond the date (Tagger: someone and some log message), I don’t know if this is a bug in GIT or some intended behaviour, but git log -1 does not do this.Enthusiastic
T
38

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

Tepic answered 6/11, 2013 at 12:52 Comment(3)
This seems to give the date of the commit a tag points to, not the date of the tag its self.Palmetto
@Palmetto tags only have metadata (like date) if it's annotatedOperant
I tried to verify that, but now I get the same result for all tags I create, annotated or not, and it is neither the tagging date nor the commits date. I guess something is wrong on my side.Palmetto
W
27

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
Washbasin answered 28/9, 2015 at 11:18 Comment(1)
Is it possible to apply this for all the commits all in once?Germinal
D
10

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
Drolet answered 14/7, 2021 at 12:57 Comment(0)
B
7

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.

Babyblueeyes answered 11/11, 2020 at 15:32 Comment(0)
T
2

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

Triste answered 22/1, 2019 at 14:19 Comment(0)
F
1

I know the question was asked a long time ago and I don't know which version of git this was.

For git version 2.37.3 the command

git show -s

will show the author date, not the commit date. To get the commit date of a commit use

git log -1 --format=fuller 'commit hash'
Fagan answered 13/10, 2022 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.