如何让R的devtools::install_github()读取gitcreds存储的GitHub凭证?
问题描述
我正在将GitHub凭证从~/.profile迁移至正规凭证管理器(运行环境为macOS Monterey 12.6)。目前我可以通过以下代码访问私有仓库:
devtools::install_github("myname/myrepo", auth_token = "my_personal_access_token")
但我希望无需每次输入个人访问令牌(PAT)或写入脚本即可完成操作(我需要频繁切换仓库标签)。我已尝试使用:
gitcreds::gitcreds_set()
执行后显示:
-> Your current credentials for 'https://github.com': protocol: https host : github.com username: myname password: <-- hidden --> -> What would you like to do? 1: Abort update with error, and keep the existing credentials 2: Replace these credentials 3: See the password / token Selection:
我选择查看密码/令牌确认其正确,但不携带PAT时执行以下代码会失败:
devtools::install_github("myname/myrepo")
报错信息:
Error: Failed to install 'myrepo' from GitHub: HTTP error 404. Not Found Did you spell the repo owner (`myname`) and repo name (`myrepo`) correctly? - If spelling is correct, check that you have the required permissions to access the repo.
我的问题是:如何让install_github()从gitcreds包中获取凭证?
以下是我的sessionInfo()供参考:
R version 4.2.1 (2022-06-23) Platform: x86_64-apple-darwin21.6.0 (64-bit) Running under: macOS Monterey 12.6 Matrix products: default LAPACK: /usr/local/Cellar/r/4.2.1_4/lib/R/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] gitcreds_0.1.2 devtools_2.4.4 usethis_2.1.6 loaded via a namespace (and not attached): [1] Rcpp_1.0.9 pillar_1.8.1 compiler_4.2.1 later_1.3.0 [5] urlchecker_1.0.1 prettyunits_1.1.1 profvis_0.3.7 remotes_2.4.2 [9] enstools_0.0.0.9000 tools_4.2.1 digest_0.6.29 pkgbuild_1.3.1 [13] pkgload_1.3.0 tibble_3.1.8 memoise_2.0.1 lifecycle_1.0.2 [17] pkgconfig_2.0.3 rlang_1.0.6 DBI_1.1.3 shiny_1.7.2 [21] cli_3.4.1 rstudioapi_0.14 curl_4.3.2 fastmap_1.1.0 [25] stringr_1.4.1 dplyr_1.0.10 generics_0.1.3 vctrs_0.4.2 [29] fs_1.5.2 htmlwidgets_1.5.4 tidyselect_1.1.2 glue_1.6.2 [33] R6_2.5.1 processx_3.7.0 fansi_1.0.3 sessioninfo_1.2.2 [37] callr_3.7.2 purrr_0.3.4 magrittr_2.0.3 ps_1.7.1 [41] promises_1.2.0.1 ellipsis_0.3.2 htmltools_0.5.3 assertthat_0.2.1 [45] mime_0.12 xtable_1.8-4 httpuv_1.6.6 utf8_1.2.2 [49] stringi_1.7.8 miniUI_0.1.1.1 cachem_1.0.6 crayon_1.5.2
解决方案
1. 修正gitcreds的凭证存储格式
devtools::install_github()依赖remotes包处理凭证,而GitHub的HTTPS验证要求用户名必须设为git(而非你的GitHub账号名)。重新设置凭证:
gitcreds::gitcreds_set(url = "https://github.com")
在交互选项中选择2替换现有凭证,然后输入:
- 用户名:
git - 密码:你的PAT
2. 配置git使用系统钥匙串
macOS下确保git使用系统钥匙串管理凭证,执行以下终端命令:
# 清除旧的凭证配置(如果有) git config --global --unset credential.helper # 设置使用osxkeychain git config --global credential.helper osxkeychain
3. 清除残留的环境变量
如果之前在~/.profile中设置过GITHUB_PAT环境变量,remotes会优先读取该变量而非git凭证。检查并移除:
# 检查是否存在GITHUB_PAT Sys.getenv("GITHUB_PAT")
如果有返回值,打开~/.profile删除相关设置,然后重启R会话。
4. 验证凭证并测试
确认凭证读取正确:
gitcreds::gitcreds_get(url = "https://github.com")
确保返回的用户名为git,密码为你的PAT。之后直接运行安装命令即可:
devtools::install_github("myname/myrepo")
5. 排查404错误的其他可能
如果仍报错,需确认:
- 仓库路径完全正确(GitHub对仓库名大小写敏感)
- PAT已勾选
repo权限(在GitHub的PAT设置页面检查) - 系统钥匙串中无旧的错误GitHub凭证,可手动删除后重新设置
内容的提问来源于stack exchange,提问作者Ngwenyama
相关产品推荐
相关产品推荐

