r语言 - 从 RStudio 初始化 GitHub 存储库,而无需使用命令行



我的任务是在我的工作地点测试 GitHub 培训计划的可行性。由于内部安全,我们不能使用命令行。我正在尝试:

  • 初始化新的 git 存储库
  • 从我当前的 R 项目添加文件
  • 提交这些暂存文件
  • 推送到新创建的 GitHub 存储库

全部来自使用 git2r 函数的 RStudio。

我在网上找到的每个教程都依赖于命令行或查看克隆现有存储库。我找不到任何从本地 r 项目创建存储库的方法。

到目前为止的工作流程如下所示:

library(git2r)
repo <- init("C:/local_folder/r_project_folder")
config(repo, user.name, user.email)
cred <- cred_token() # Git PAT is included in the projects renviron file
add(repo, "*")
commit(repo, "Commit message")
push()

返回错误消息:

Error in 'git2r_push': remote 'origin' does not exist

如果只是有一个指向综合教程的链接,那将对我有所帮助。git2r 自述文件侧重于连接到现有存储库。

您遇到的错误是因为您没有为 git 提供推送到的位置。

我认为您正在寻找remotes命令 - https://www.rdocumentation.org/packages/git2r/versions/0.23.0/topics/remotes

您可以为此提供本地文件夹路径,它不必是远程服务器。

使用一点谷歌和一点汉塞尔·帕伦西亚斯的建议来解决:

首先在存储库页面上创建一个空的 GitHub 存储库,然后输入以下命令:

# Clone existing github repo using url and linking this to your project path
clone(url = "https://github.com/user/name_of_repo.git", local_path = "path")
setwd("path")
# Assign variable repo as current local repository
repo <- repository()
# Configure access rights with github username and profile
config(repo, user.name='user name', user.email='email address')
# Add a PAT in local renviron folder
edit_r_environ()
# Save PAT as cred token
cred <- cred_token()
# Stage changs to commit
add(repo, "file.R")
commit(repo, "Commit message")
# Push changes
push(repo, 'origin', 'refs/heads/master', credentials = cred)

有点黑客,但解决了问题。将尝试使用建议的 remotes(( 命令作为更有效的解决方案。

最新更新