How to get SHA of the latest commit from remote git repository?

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.


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 

Tags:

Git