我的目标是在默认分支中获取最新 SHA 的树
GET /repos/:owner/:repo/git/trees/:sha
如何从默认分支中找到最新的 SHA?
我知道我可以打电话
GET /repos/:owner/:repo/branches/:branch
但我不能只使用“master”作为分支,因为并非所有回购协议都使用 master 作为默认分支。
我如何找出回购的默认分支是什么?
调用
/repos/:owner/:repo
并读取 default_branch
属性值——这是默认分支的名称。请参阅此处的示例响应:http://developer.github.com/v3/repos/#get
这现在也可以通过 github cli 以及
gh repo list <Your_Name> --json nameWithOwner,defaultBranchRef
如果你想稍微清理输出,你可以用jq
重新映射gh repo list <Your_Name> --json nameWithOwner,defaultBranchRef \
--jq ".[] | { nameWithOwner , defaultBranch: .defaultBranchRef.name}"
这种方法的优点是集成了身份验证并且更易于管理
这是使用 gh 的更干净、更快速的方法:
gh api repos/{owner}/{repo} --jq '.default_branch'
(在 Git Bash 中前导斜线导致它被解释为本地绝对路径)