Github API: Get commits for a specific branch, excluding commits from the master branch

308 Views Asked by At

I have 2 branches: master and test-git, test-git was created from master branch. Master branch has 3 commits, test-git is 2 commits ahead. I'm trying to get only these 2 commits by using GET /repos/{owner}/{repo}/commits?sha=test-git endpoint. But I get all 5 commits. Is there a way to get only specific 2 commits, excluding all master branch commits? Here's the code I use:

const commits = await octokit.request('GET /repos/{owner}/{repo}/commits?sha=test-git', {
    owner: 'alopwer',
    repo: 'vue-countries',
    headers: {
      'X-GitHub-Api-Version': '2022-11-28'
    }
}).then(data => data.data);

I've tried to search for similar questions, but they don't solve my issue.

1

There are 1 best solutions below

0
Alopwer On

I used the compare endpoint, which returns all commits the branch is ahead: https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#compare-two-commits