【问题标题】:Get the repository default branch of a GitHub repository [duplicate]获取 GitHub 存储库的存储库默认分支 [重复]
【发布时间】:2016-02-13 15:57:11
【问题描述】:

我正在尝试使用 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 存储库默认分支的正确方法是什么。

【问题讨论】:

    标签: git github github-api


    【解决方案1】:

    存储库对象包含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 值。

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 1970-01-01
      • 2013-05-06
      • 2020-03-16
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 2011-03-08
      相关资源
      最近更新 更多