35

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.

2
  • I'm using Rest API v3 Commented Aug 17, 2017 at 3:17
  • Why wouldn't /repos/:owner/:repo/commits/:branch be the latest commit? Commented Aug 17, 2017 at 3:18

4 Answers 4

58

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.

Sign up to request clarification or add additional context in comments.

7 Comments

How does one use the GET idea with the api. How is it included in the curl?
But I need to pass an owner. How do I know that that dev was the last one pushing something to the repo?
@Dani Why passing the owner of the repository a problem in your case?
@SridharSarnobat True, but do not use your GHE account password, use a token: github.blog/changelog/… (nov. 2020)
|
12

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}"

1 Comment

the "Accept" header is important there to just get the SHA, see docs.github.com/en/rest/reference/repos#get-a-commit
11

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

1 Comment

Wow, you don't even have to authenticate. Thanks for sharing this!
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.