'How to get latest commit hash from remote?

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?

git


Solution 1:[1]

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`

Solution 2:[2]

You can use git show with remotes:

git show origin/master

Will show you the last commit of origin/master

EDIT: To get the "absolute" latest commit, I'd do something along the lines of:

git log -n 1 $(git branch -r)

Which will log all the remote branches (git branch -r). But will keep just the first commit (-n 1).

I am not too sure of the chronological order, but on my repo is seems to give me the latest one.

Another option, more "oneliner-like" would be:

git show -s --pretty='format:%ci %s %h' $(git branch -r) | sort -r | head -n 1

Which will list the last commits of each remote branch formatted in a way that the date is first, then sorted (on the date) and keeping the first

Solution 3:[3]

Fetch from the remote, "origin" for example,

git fetch origin

Print the information of all remote branches under "refs/remotes/origin",

git for-each-ref refs/remotes/origin

Sort by committer date in descending order,

git for-each-ref refs/remotes/origin --sort="-committerdate"

Get the latest,

git for-each-ref refs/remotes/origin --sort="-committerdate" | head -1

If you consider tags too, the above result may not be the latest commit. To find out the latest commit pointed at by tags,

git for-each-ref refs/tags --sort="-*committerdate" | head -1

Note that a tag may be annotated, so here * is necessary.

You might be also interested in refs under other namespaces, like abandoned pull requests. Compare the latest commits of each group of refs and get the latest one among them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcus
Solution 2
Solution 3