获取 GitHub 存储库的存储库默认分支

Get the repository default branch of a GitHub repository

我正在尝试使用 GitHub API 获取指定存储库 (owner/repo) 的默认分支。

我没有找到与此相关的任何文档。要获得我使用的所有分支:

curl https://api.github.com/repos/owner/owner/branches

这会输出以下内容 json:

[
  {
    "name": "docs",
    "commit": {
      "sha": "a3...",
      "url": "https://api.github.com/repos/owner/repo/commits/a3..."
    }
  },
  {
    "name": "master",
    "commit": {
      "sha": "8c...",
      "url": "https://api.github.com/repos/owner/repo/commits/8c..."
    }
  }
]

这已经很有用了,因为在我的例子中,默认分支是 master(如果存在)或 gh-pages。但我仍然想知道获取 GitHub 存储库默认分支的正确方法是什么。

存储库对象包含 default_branch 字段。

所以,我可以这样做:

curl https://api.github.com/repos/owner/repo

{
  "id": 29...,
  "name": "repo",
  "full_name": "owner/repo",
  "owner": {...},
  "private": false,
  ...
  "default_branch": "master",
  ...
}

在这种情况下,default_branch 具有 master 值。