93

I need the last commit date in git. This means the latest update date in my program.

I used the command : $ git log -1 but this command will give me the date from the local repository. Rather I need date from remote repository.

I tried some commands as follow.

git log -n 1 origin/Sprint-6.
git rev-parse --verify HEAD
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Narendra Vadnere
  • 931
  • 1
  • 6
  • 4
  • 1
    you can always use ```gitk --all``` to open the tree view for all branches (including remotes) – mTorres Aug 29 '14 at 07:32

6 Answers6

49

Get the last commit date:

You want the "repository wide last commit date for a given git user and git project, for a given branch. For example the date is shown at the top when you visit your repo and go to commits -> master for example:

https://github.com/sentientmachine/TeslaAverageGainByMonthWeekDay/commits/master

Get the last local commit date in git using terminal

Use git help log for more info on format codes to pass to --format to tell git log what kind of data to fetch.

The last commit date in git:

git log -1 --format="%at" | xargs -I{} date -d @{} +%Y/%m/%d_%H:%M:%S
#prints 2018/07/18 07:40:52

But as you pointed out, you have to run that command on the machine that performed the last commit. If the last commit date was performed on another machine, the above command only reports local last commit... So:

Or Repository wide: Get the last git commit date

Same as above, but do a git pull first

git pull; 
git log -1 --format="%at" | xargs -I{} date -d @{} +%Y/%m/%d_%H:%M:%S
#prints 2018/07/18 09:15:10

Or use the JSON API:

Doing git pulls is very slow and you're banging GitHub with a heavy operation. Just query the GitHub rest api:

#assuming you're using github and your project URL is visible to public:
# https://github.com/yourusername/your_repo_name

#then do:
curl https://api.github.com/repos/yourusername/your_repo_name/commits/master

That blasts you in the face with a screen full of json, so send it your favorite json parser and get the field called date:

curl https://api.github.com/repos/<your_name>/<your_repo>/commits/master 2>&1 | \
grep '"date"' | tail -n 1
#prints "date": "2019-06-05T14:38:19Z"

From comments below, gedge has handy dandy improvements to incantations:

git log -1 --date=format:"%Y/%m/%d %T" --format="%ad"
2019/11/13 15:25:44

Or even simpler: ( https://git-scm.com/docs/git-log/1.8.0 )

git --no-pager log -1 --format="%ai"
2019-12-13 09:08:38 -0500

Your choices are north, south, east, and "Dennis".

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 2
    Just curious; any special reason for using `xargs`? I am thinking `date -d @\`git log -1 --format="%at"\`` will be a more concise solution. – papigee Oct 12 '18 at 15:09
  • 3
    My choice of `xargs` was a blend of google-fu, guess and check, and brownian motion. Your solution appears to remove unnecessary layers of the onion. Have an up boat. – Eric Leschinski Oct 12 '18 at 15:27
  • For Mac OSX, date is different and will not work. You might want to use `DT=\`git log -1 --format="%at"\` && python -c "import time; print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime($DT)))"` – ishahak Jan 02 '19 at 14:17
  • 2
    This osx command needs more layers. Maybe you can add a javascript ajax hit to mongodb so that is more webscale. – Eric Leschinski Jan 02 '19 at 14:23
  • 10
    sarcasm when using `xargs...date`? when this would do: `git log -1 --date=format:"%Y/%m/%d %T" --format="%ad"` – Gedge Jul 05 '19 at 13:48
  • As `date` is a `one-liner`, can we output without `less-style` just printing it and the cursor moves to the beginning of a new terminal line ready to rumble? – Timo May 09 '21 at 07:12
  • At the end of Erics answer you see a nice flag to output without `less`: `--no-pager`. – Timo May 13 '21 at 08:13
7

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

  • Command: git log -1 --format=%ct
  • Result: 1605103148

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

Keshav Lodhi
  • 2,641
  • 2
  • 17
  • 23
4

git log -1 will give you the Merge id, Author and Date

git log -1 --format=%cd will give the output as below

Wed Apr 13 15:32:54 2022 +0530

We can format the date as below: git log -1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S'

output 2022-04-13 15:32:54

  • 1
    Hello @Divya - thank you for your contribution, but your answer is a duplicate of already existing answers. – KJH May 25 '22 at 21:59
2

Another oneliner for Linux, get UTC ISO 8601 time formatted up to the minutes:

TZ=utc date -d @$(git log -1 --format=%ct) --iso-8601=m

Gives 2022-12-07T10:01+00:00

FredG
  • 712
  • 7
  • 10
1

Late to the party but here's how to get the UNIX timestamp of the latest remote commit:

git log -1 --date=raw origin/master | grep ^Date | tr -s ' ' | cut -d ' ' -f2
vesperto
  • 804
  • 1
  • 6
  • 26
0

Using git version 2.39.2 .

As I set up my ~/.gitconfig with showSignature = true, executing git log -1 --format=%cI will additionally include commit signature information, e.g.

Good "git" signature for abdull@example.com with RSA key SHA256:abcd
2023-04-14T20:02:23+02:00

To remove the commit signature information, add the --no-show-signature flag, e.g. git log --no-show-signature -1 --format=%cI:

2023-04-14T20:02:23+02:00
Abdull
  • 26,371
  • 26
  • 130
  • 172