GitHub

2022-05-06

GitHub 是全球最大的开源代码托管平台和开发者社区(同性交友网站),使用 [[Git]] 作为版本控制,2018 年被微软收购。

使用

连接

查看 ~/.ssh 目录下是否有

Terminal window
# 创建,一路回车
ssh-keygen -t ed25519 -C "[email protected]"
# 拷贝 pub 文件内容到剪贴板
cat id_ed25519.pub | pbcopy

拷贝后到 GitHub -> Setting -> SSH and GPG keys 粘贴即可,其他问题可参考 使用 SSH 连接到 GitHub

技巧

  1. 在项目界面按. 键,在网页编辑器([[VSCode]])中打开浏览;
  2. 在项目地址前加 gitpod.io/#/,在线运行项目([[VSCode]]);

API

如果是私有仓库需要从:Settings -> personal access tokens -> Fine-grained tokens 创建 Token。

选择所需仓库,然后权限选择:

  1. Contents
  2. Metadata

获取仓库文件

仅获取指定目录:/repos/{owner}/{repo}/contents/{path}

Terminal window
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/contents/PATH

递归获取所有文件:/repos/{owner}/{repo}/git/trees/{tree_sha}

Terminal window
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/git/trees/master

可加路径参数:

  • recursive: 1 或 true 为递归获取

获取文件内容

通过 contents 接口获取:/repos/{owner}/{repo}/contents/{file}

Terminal window
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/contents/file

这个接口会返回文件元数据和内容(Base64 编码)

通过 raw 接口获取:`/{owner}/{repo}/{branch}/{path}

Terminal window
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://raw.githubusercontent.com/OWNER/REPO/master/file

这个接口直接返回原始内容,不需要再通过 Base64 解码。

通过 Blob 接口获取:/repos/{owner}/{repo}/git/blobs/{sha}

Terminal window
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/git/blobs/sha

需要文件的 sha,返回的内容同样包含元数据和通过 Base64 编码的内容。

问题

GnuTLS recv error (-110): The TLS connection was non-properly terminated

Terminal window
apt-get install gnutls-bin
git config --global http.sslVerify false
git config --global http.postBuffer 1048576000

[[Ubuntu]] 下成功解决。

参考