You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过GitHub API获取test-git分支独有的提交(排除master分支)

解决方案

要只获取test-git分支相对于master分支独有的2次提交,你需要在API请求中添加base参数,指定对比的基准分支为master。

GitHub的GET /repos/{owner}/{repo}/commits接口支持通过sha和base参数组合,返回两个分支之间的差异提交——也就是sha分支有但base分支没有的提交。

修改你的代码如下:

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

为什么原来的请求不行?

只指定sha=test-git时,API会返回该分支历史中的所有提交,包括从master分支继承过来的3次提交。加上base=master后,API会自动计算两个分支的提交差异,只返回test-git超前于master的那2次提交。

内容的提问来源于stack exchange,提问作者Alopwer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 00:31:59