tags:

views:

453

answers:

4

Does anyone know how to get the latest SHA of a given branch FROM OUTSIDE a git repository?

If you are inside git repository, it’s you can do: git log origin/branch_X | head -1

However, I am not inside a git repository, and I would like to avoid having to “clone” a repository just to get the latest SHA of a tag/branch. Is there a clever way of doing this?

Thanks!

-Steve

+1  A: 

A colleague of mine answered this for me:

git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git

-Steve

AdvilUser
A: 

References to branch heads are stored in the .git/refs/ tree. So you should be able to find the hash of the latest commit at:

cat .git/refs/remotes/origin/branch_X

Your path may differ slightly.

Greg Hewgill
That wouldn't work if you have packed refs. Then you have to take a look at .git/packed-refs
Jakub Narębski
That's true, this solution is susceptible to any future changes in the Git on-disk repository format.
Greg Hewgill
+3  A: 

If you want to check SHA-1 of given branch in remote repositoy, then your answer is correct:

$ git ls-remote <URL>

However if you are on the same filesystem simpler solution (not requiring to extract SHA-1 from output) would be simply:

$ git --git-dir=/path/to/repo/.git rev-parse origin/branch_X

See git(1) manpage for description of '--git-dir' option.

Jakub Narębski
A: 

If you just want the SHA-1 from the currently checked out branch of your local repo, you can just specify HEAD instead of origin/branch_X:

git --git-dir=/path/to/repo/.git rev-parse --verify HEAD