Does anyone know how to get the latest SHA of a given branch from outside a git repository?
If you are inside a git repository, you can do:
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
Use rev-parse
git rev-parse origin/master # to get the latest commit on the remote
git rev-parse HEAD # to get the latest commit on the local
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.
Using a git URL:
$ git ls-remote <URL> | head -1 | sed "s/HEAD//"
Using a directory on an accessible system:
$ git --git-dir=/path/to/repo/.git rev-parse origin/<targeted-banch>
A colleague of mine answered this for me:
git ls-remote ssh://git.dev.pages/opt/git/repos/dev.git <branch>
If you want to check SHA-1 of given branch in remote repository, 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.