Skip to content

Instantly share code, notes, and snippets.

@bitrut
Created December 18, 2011 20:13
Show Gist options
  • Save bitrut/1494315 to your computer and use it in GitHub Desktop.
Save bitrut/1494315 to your computer and use it in GitHub Desktop.
Get timestamp of the last commit in git repository
#!/usr/bin/python
import subprocess
import re
from optparse import OptionParser
def git_version():
p = subprocess.Popen(["git", "log" , '-1', '--date=iso'], stdout=subprocess.PIPE)
out, err = p.communicate()
m = re.search('\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}', out)
return m.group(0)
usage = "usage: %prog -f filepath"
parser = OptionParser(usage=usage)
parser.add_option("-f", default='version.py')
(options, args) = parser.parse_args()
path = options.f
with open(path, 'w+') as f:
f.write(git_version())
@hilbix
Copy link

hilbix commented Dec 7, 2012

What about that?

git rev-list --format=format:'%ai' --max-count=1 `git rev-parse HEAD`

or

git rev-list --format=format:'%ci' --max-count=1 `git rev-parse HEAD`

@nlevitt
Copy link

nlevitt commented May 30, 2014

$ git log -1 --date=short --pretty=format:%cd
2014-05-29

@weakish
Copy link

weakish commented Oct 17, 2014

And for unix time:

git log -1 --pretty=format:%ct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment