29

Which is the best way to get the latest commit information from a git repository using GitHub API (Rest API v3).

Option 1: GET /repos/:owner/:repo/commits/master
Can I assume that the object 'commit' of the response is the latest commit from branch master?

Option 2: GET /repos/:owner/:repo/git/commits/5a2ff
Or make two calls, one to get the sha by getting the HEAD ref from master and then get the commit information using the sha returned.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Rogelio Blanco
  • 1,462
  • 4
  • 19
  • 29

4 Answers4

53

It depends on your definition of "last".

  • for a given branch (like master), GET /repos/:owner/:repo/commits/master is indeed the last (most recent) commit.

  • But you can also consider the last push event: that would represent the last and most recent commit done (on any branch), pushed by a user to this repo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • How does one use the GET idea with the api. How is it included in the curl? – John Wooten Apr 10 '20 at 16:07
  • @JohnWooten See for examples https://gist.github.com/joyrexus/85bf6b02979d8a7b0308 – VonC Apr 10 '20 at 16:37
  • But I need to pass an owner. How do I know that that dev was the last one pushing something to the repo? – Dani Aug 17 '20 at 08:08
  • 1
    @Dani Why passing the owner of the repository a problem in your case? – VonC Aug 17 '20 at 08:26
  • Ah sorry, I thought it was the owner of the commit. That works then – Dani Aug 17 '20 at 09:38
  • `curl --user username:mypassword "https://mygithubenterprise/api/v3/repos/myorg/myproj/commits/master"` – Sridhar Sarnobat Jun 04 '21 at 22:02
  • 1
    @SridharSarnobat True, but do not use your GHE account password, use a token: https://github.blog/changelog/2020-11-13-token-authentication-required-for-api-operations/ (nov. 2020) – VonC Jun 05 '21 at 16:03
10

Another method to getting the latest commit from a user would be using the following endpoint. To clarify, this will only show public events so pushes to a private repository will not be shown.

https://api.github.com/users/<username>/events/public
pythonNovice
  • 1,130
  • 1
  • 14
  • 36
8

If you only need the SHA1 of the latest commit of a certain branch here's a curl request that will do it:

curl -s -H "Authorization: token {your_github_access_token}" \
-H "Accept: application/vnd.github.VERSION.sha" \ 
"https://api.github.com/repos/{owner}/{repository_name}/commits/{branch_name}"
Anton
  • 1,793
  • 10
  • 20
7

You can also use Github GraphQL v4 to get the last commit of the default branch :

{
  repository(name: "linux", owner: "torvalds") {
    defaultBranchRef {
      target {
        ... on Commit {
          history(first: 1) {
            nodes {
              message
              committedDate
              authoredDate
              oid
              author {
                email
                name
              }
            }
          }
        }
      }
    }
  }
}

Or for all branches :

{
  repository(name: "material-ui", owner: "mui-org") {
    refs(first: 100, refPrefix: "refs/heads/") {
      edges {
        node {
          name
          target {
            ... on Commit {
              history(first: 1) {
                nodes {
                  message
                  committedDate
                  authoredDate
                  oid
                  author {
                    email
                    name
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Try it in the explorer

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159