GitHub 个人访问令牌(PAT)生成与 R 环境配置
1. 生成 GitHub 个人访问令牌(PAT)
- 访问 GitHub 设置页面:打开浏览器,访问 GitHub 的 Personal Access Tokens 生成页面。
- 登录 GitHub 账户:输入用户名和密码登录。
- 生成新的个人访问令牌(PAT):
- 在 Personal access tokens 页面,点击 Generate new token。
- 填写以下内容:
- Note:命名令牌,例如
R-Access-Token。 - Expiration:选择过期时间(如 30 天、90 天或 No expiration)。
- Select Scopes:为通过 Git 安装包,至少选择以下权限:
repo(允许访问私有仓库)workflow(允许访问 GitHub Actions)read:org(读取组织信息,必要时选择)write:packages(写入包,必要时选择)read:packages(读取包,必要时选择)
- Note:命名令牌,例如
- 生成并保存令牌:点击 Generate token 按钮。生成后务必复制并保存该令牌,因为它只会显示一次。
2. 配置 GitHub 凭证
在 R 中配置凭证
使用 gitcreds 包管理 GitHub 凭证。
install.packages("gitcreds")
library(gitcreds)
执行以下函数将 PAT 设置到 GitHub 凭证中:
gitcreds::gitcreds_set()
按提示输入 GitHub 用户名和生成的 PAT。
配置 Git 远程 URL 和凭证
在终端中确保 Git 使用 HTTPS 协议访问 GitHub,并缓存凭证。
- 配置远程 URL:
git remote set-url origin https://github.com/username/repository.git
将 username/repository 替换为实际的用户名和仓库名。
- 启用凭证缓存:
git config --global credential.helper store
执行 Git 操作时,Git 会提示输入用户名和 PAT,随后自动缓存。


