Jason C
Jason C

Reputation: 40305

How to retrieve commit hash of latest available version in repo

I know how to get the commit hash for the current version of the local repo (How to retrieve the hash for the current commit in Git?).

Given a branch name, possibly different from the local repos branch, is there a way to get the long hash of the latest available version in the remote repo without actually doing a clone or pull?

Upvotes: 1

Views: 894

Answers (2)

torek
torek

Reputation: 487725

git ls-remote (or git remote show but it's not directly useful here) can use the same Git protocols as git clone and git fetch to contact another Git. These begin by getting a list of reference-to-hash mappings, e.g.:

8d7a455ed52e2a96debc080dfc011b6bb00db5d2        HEAD
a274e0a036ea886a31f8b216564ab1b4a3142f6c        refs/heads/maint
8d7a455ed52e2a96debc080dfc011b6bb00db5d2        refs/heads/master
fb549caa12ec67f9448f8f6b29b102ee5ec54c82        refs/heads/next
4b8cb2c9d27c63c844b1d2507b8b0981adfcf397        refs/heads/pu
595ca97928bf3cde17e40ecbbacb65cc3d053a06        refs/heads/todo

With clone and fetch, they go on to download any "interesting" objects, while git ls-remote simply prints them out and then stops.

See the documentation for options and additional details; note that the repository argument can be a URL, so you do not even need a clone to run in.

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

I don't know of a way to directly do this using only Git locally. If you are using a tool like Bitbucket or GitHub, then the website would show the latest commit hash in plain view.

One option to do this locally without having a lasting impact on anything would be to simply update your tracking branch. But first, record what the commit hash of the tracking branch is. So do this:

git log origin/master    # record the SHA-1 hash from HEAD
git pull origin master   # update the tracking branch
# find the SHA-1 hash you want
git update-ref refs/remotes/origin/master <original SHA-1 hash of origin/master>

The trick here is to reset the tracking branch to the point where it was when you started. Now you have the hash you want.

But question: What use is this hash if you don't have a tracking branch containing the corresponding commits?

Upvotes: 1

Related Questions