101

I want to get the last commit ID of the remote git repo.

The command git rev-parse HEAD works for a locally-cloned git repo, but I want to get it from the original GIT repo by a CURL command or so.

Eg: I want to get the last commit ID of the git URL https://git.appfactorypreview.wso2.com/history/apiapp.git/.

How?

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Manisha Eleperuma
  • 1,089
  • 2
  • 7
  • 7

8 Answers8

158

try this command

git log --format="%H" -n 1
Litmus
  • 10,558
  • 6
  • 29
  • 44
54

Another way, without using git log:

git rev-parse HEAD

Bruno Duyé
  • 1,014
  • 11
  • 13
51

I think what you want is this:

git ls-remote $URL HEAD

If HEAD doesn't exist in the remote repository, then you likely want:

git ls-remote $URL refs/heads/master

Note that in the first instance, HEAD is going to point to the default branch to checkout in the repository. You need to be sure that's the branch you want, or just use the second form and specify the one you want (replace refs/heads/master with the name of the branch you want: refs/heads/BRANCH_NAME.

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
  • You cannot use `HEAD`, because it is a pointer to the current branch. But in a bare repo it does not exists a `HEAD`. – silvio Oct 04 '13 at 08:42
  • 2
    It's not true that it *never* exists. Case and point: `git ls-remote git://github.com/jszakmeister/vimfiles.git HEAD`. In a bare repo, it tells Git which branch to checkout as the default branch. It is true that you cannot count on it existing. So, in that case you should use an appropriate refname. I'll update my answer. – John Szakmeister Oct 04 '13 at 08:59
17

You can use git ls-remote for this. Because I get a 'Unauthorized access for repository apiapp.git' I use as example torvalds linux-repo.

$ git ls-remote --heads git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
6d15ee492809d38bd62237b6d0f6a81d4dd12d15        refs/heads/master
silvio
  • 2,174
  • 20
  • 36
14

The short hash of the last commit id is much more human readable (read: user friendly). For posterity, two ways to get the short hash of the last commit id:

git rev-parse --short HEAD

OR

For getting short hash eg. fb8a7de

git log -n1 --format="%h"

For getting full hash eg. fb8a7decf471abc61dc6e49616697d3bd722b96f

git log -n1 --format="%H"

You can find more information on pretty-formats here https://git-scm.com/docs/pretty-formats

Tanuj
  • 2,032
  • 10
  • 18
rouble
  • 16,364
  • 16
  • 107
  • 102
7

Simplest way I use:

git rev-parse origin/develop
codelovesme
  • 3,049
  • 1
  • 16
  • 18
4

my answer would not help the OP because he's not on github, but I think I would mention it anyway because it uses curl, or wget, as the OP requested.

wget -qO- http://api.github.com/repos/Ghini/ghini.desktop/commits/ghini-1.0

Ghini is my repo, ghini.desktop is my repository, ghini-1.0 is the branch I'm interested in. Replace them to fit your case.

the JSON answer is a dictionary, and the OP was interested in its sha field, but it contains a lot more information.

mariotomo
  • 9,438
  • 8
  • 47
  • 66
  • Thanks, this helped me a lot. Have a look at this :) https://gist.github.com/AiyionPrime/40dbe9bd3be05bd68e25c5c0e1bfa92e#file-get_commit-sh – Aiyion.Prime Apr 10 '20 at 09:06
0

git fetch; git rev-parse origin/branch_name

To be safe, run git fetch first.

Fisher
  • 376
  • 3
  • 14