Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get last commit date from git repository?

Tags:

git

git-commit

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 
like image 841
Narendra Vadnere Avatar asked Aug 29 '14 07:08

Narendra Vadnere


People also ask

How do I find the last git pull date?

You can also check . git/refs/heads/master which will have its timestamp change when git pull results in changes coming in from the remote master branch, but the timestamp won't change when git pull reports there are no changes.

How do I see commit history?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.


2 Answers

The following command will be helpful:

git log -1 --format=%cd  

This will print the latest change date for one file. The -1 shows one log entry (the most recent), and --format=%cd shows the commit date. See the documentation for git-log for a full description of the options.

like image 168
love Avatar answered Sep 26 '22 18:09

love


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.

The date you're after is the latest date shown when you visit your repo and go to commits -> master for example:

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

The top of the page shows the latest commit date.

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 that 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 (if the repository is hosted in GitHub) use the JSON API:

git pulls on a schedule aren't cool because it's slow and you're banging GitHub with unnecessary network traffic. 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".

like image 22
Eric Leschinski Avatar answered Sep 25 '22 18:09

Eric Leschinski