Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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:

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?

like image 367
AdvilUser Avatar asked Jul 21 '09 21:07

AdvilUser


People also ask

How do you get commit ID of last commit in git?

To find a git commit id (or hash), you can simply use the git log command. This would show you the commit history, listing the commits in chronological order, with the latest commit first.


2 Answers

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  
like image 58
gprasant Avatar answered Sep 17 '22 12:09

gprasant


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.

like image 27
Jakub Narębski Avatar answered Sep 17 '22 12:09

Jakub Narębski