How can I get the SHA of a GitHub commit in Java?

I’d like to know if there is a hacky method to obtain a commit SHA of the latest commit of a branch in a particular GitHub repo in Java. As far as I have seen, the GitHub API only has a hook to access the full commit info, not just the API. Anybody have any ways to do this?

  • How do I git add only lines matching a pattern?
  • What is the correct address for an image in a markdown document in VSTS?
  • Why can I not commit? (Your branch is up-to-date with 'origin/master', no changes added to commit)
  • Git equivalent of RCS co -p?
  • Using Git in a TFS shop
  • Why is jGit's RevWalk.reset() method misbehaving?
  • Git Revision Expression
  • How to run ./ in windows Git shell?
  • SSL certificate rejected trying to access GitHub over HTTPS behind firewall
  • How to find who last changed a file with JGit
  • git workflow with vendor directory
  • Git: can't switch to new remote branch
  • 2 Solutions collect form web for “How can I get the SHA of a GitHub commit in Java?”

    Use the refs api, as suggested by cjc343:

    GET /repos/:owner/:repo/git/refs/:ref

    For example:

    $ curl
      "ref": "refs/heads/master",
      "url": "",
      "object": {
        "sha": "d991ef2ab1b4d156c7e5d33d052d66f8eaafc460",
        "type": "commit",
        "url": ">/d991ef2ab1b4d156c7e5d33d052d66f8eaafc460"

    You can either extract object.sha after parsing this as JSON or, in the spirit of “hacky”, pipe through:

    | grep \"sha\": | cut -f4 -d\"

    to get:


    This may not be exactly what you want (no Java). However, using java based ssh-api and the following commands you can achieve this.

    git ls-remote<github username>/<git project name>.git

    Following is a sample output:

    4de51ba41fba357ac9ce63f098451cd1fc2dacbe        HEAD
    4de51ba41fba357ac9ce63f098451cd1fc2dacbe        refs/heads/master
    cc42dc12a972dab122030b046440f47e0485e321        refs/pull/1/head
    73629fe86def6fef896abd32637b97fddf8cd3b4        refs/pull/1/merge

    You could of-course filter this by appending desired branch name or HEAD at the end of above command.

    git ls-remote<github username>/<git project name>.git <branch-name>


    git ls-remote<github username>/<git project name>.git HEAD

    Git Baby is a git and github fan, let's start git clone.