How to get the last commit ID of a remote repo using curl-like command?

GitGit Commit

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

try this command

git log --format="%H" -n 1

Solution 2 - Git

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.

Solution 3 - Git

Another way, without using git log:

git rev-parse HEAD

Solution 4 - Git

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

Solution 5 - Git

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

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

Solution 6 - Git

Simplest way I use:

git rev-parse origin/develop

Solution 7 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManisha EleperumaView Question on Stackoverflow
Solution 1 - GitLitmusView Answer on Stackoverflow
Solution 2 - GitJohn SzakmeisterView Answer on Stackoverflow
Solution 3 - GitBruno DuyéView Answer on Stackoverflow
Solution 4 - GitsilvioView Answer on Stackoverflow
Solution 5 - GitroubleView Answer on Stackoverflow
Solution 6 - GitcodelovesmeView Answer on Stackoverflow
Solution 7 - GitmariotomoView Answer on Stackoverflow