Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get latest commit hash from remote?

Tags:

git

I wish to retrieve the latest commit hash on the remote repository, regardless of branch.

I've tried git ls-remote <remote> and git ls-remote --tags <remote> but both of these seemingly sort by tag name, which does not make it possible to find out which is the latest.

For example on Github, you can go to Insights / Network and get a graph that includes all branches and commits in these -- however, working in that gui is obviously not ideal - but the data should be there somehow.

Is there a way to get the latest commit hash from remote irregardless of branch?

like image 793
cbll Avatar asked Dec 22 '22 23:12

cbll


1 Answers

Recent versions of git (don't know which - 2.14 didn't, 2.21 does) allow a "--sort" option in ls-remote. You can easily do:

git ls-remote --sort=committerdate

But be aware - if it is necessary to look into objects to achieve the sorting (as in the above example), the objects must be locally available. Otherwise you will get an error message "fatal: missing object". So make sure you always do a git fetch before you use it.

To be honest - since you must make sure to have all remote branches locally fetched before being able to use this, this is not much better than simply doing git branch -r --sort=committerdate after a fetch. Only obvious difference is that ls-remote directly shows the commit hash you are searching for, while with git branch you will have to wrap into a git rev-parse like this:

git rev-parse `git branch -r --sort=committerdate | tail -1`
like image 106
Marcus Avatar answered Dec 26 '22 11:12

Marcus