获取GitHub存储库的存储库默认分支 [英] Get the repository default branch of a GitHub repository

查看:41
本文介绍了获取GitHub存储库的存储库默认分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use the GitHub API to get the default branch of a specified repository (owner/repo).

我没有找到任何有关此的文档.要获得所有我使用的分支机构:

I didn't find any docs regarding that. To get all branches I use:

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

这将输出以下json:

This outputs the following 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存储库的默认分支的正确方法是什么.

This is already useful because in my case, the default branch is either master (if exists) or gh-pages. But I still want to know what's the correct way to get the default branch of a GitHub repository.

推荐答案

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

In this case, the default_branch has the master value.

这篇关于获取GitHub存储库的存储库默认分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆