Windows上使用R将本地repo推送到github

2024-01-22

我曾经问过一个很类似的问题 https://stackoverflow.com/questions/14912161/repo-from-rstudio-to-github并得到了从命令行有效的响应,但我现在想使用 R 从 Windows 自动化该过程(Linux 更容易)。

这是我想做的:

  1. 创建本地目录(或者已经存在)
  2. 在云端生成一个与本地同名的新 github 存储库(基于这个答案 https://stackoverflow.com/questions/15042418/send-to-github-via-curl-command-line-windows)
  3. 将 .git 添加到本地存储库
  4. 进行初始提交
  5. 建立云仓库和本地仓库之间的链接
  6. 将commit和本地repo中的文件推送到github

我相信基于输出 https://dl.dropboxusercontent.com/u/61803503/Errors/github_push.txt在我失败之前我已经完成了第 5 步(因为本地目录中的提交和文件永远不会到达云中的 github)。我知道步骤 2 有效,因为创建了空存储库here https://github.com/trinker/foo5。我不知道如何测试第5步。在最后一步shell(cmd6, intern = T)RGUI 和 RStudio 导致了永恒的死亡螺旋。问题是:如何将提交和本地存储库推送到云端。

这是我更新的代码(唯一特定于用户的是第三个代码块中的用户名和密码):

## Create Directory
repo <- "foo5"
dir.create(repo)
project.dir <- file.path(getwd(), repo) 

## Throw a READ.ME in the directory
cat("This is a test", file=file.path(project.dir, "READ.ME"))

## Github info (this will change per user)
password <-"pass" 
github.user <- "trinker"  

## Get git location
test <- c(file.exists("C:/Program Files (x86)/Git/bin/git.exe"),
    file.exists("C:/Program Files/Git/bin/git.exe"))
gitpath <- c("C:/Program Files (x86)/Git/bin/git.exe",
  "C:/Program Files/Git/bin/git.exe")[test][1]

## download curl and set up github api
wincurl <- "http://curl.askapache.com/download/curl-7.32.0-win64-ssl-sspi.zip"
url <- wincurl
tmp <- tempfile( fileext = ".zip" )
download.file(url,tmp)
unzip(tmp, exdir = tempdir())       
shell(paste0(tempdir(), "/curl http://curl.haxx.se/ca/cacert.pem -o " , 
    tempdir() , "/curl-ca-bundle.crt"))
json <- paste0(" { \"name\":\"" , repo , "\" } ") #string we desire formatting
json <- shQuote(json , type = "cmd" )
cmd1 <- paste0( tempdir() ,"/curl -i -u \"" , github.user , ":" , password , 
    "\" https://api.github.com/user/repos -d " , json )

shell(cmd1, intern = T)

## Change working directory
wd <- getwd()
setwd(project.dir)

## set up the .git directory
cmd2 <- paste0(shQuote(gitpath), " init")
shell(cmd2, intern = T)

## add all the contents of the directory for tracking
cmd3 <- paste0(shQuote(gitpath), " add .")  
shell(cmd3, intern = T)       

cmdStat <- paste0(shQuote(gitpath), " status")  
shell(cmdStat, intern = T)

## Set email (may not be needed)
Trim <- function (x) gsub("^\\s+|\\s+$", "", x) #remove trailing/leading white 

x <- file.path(path.expand("~"), ".gitconfig")
if (file.exists(x)) {
    y <- readLines(x)
    email <- Trim(unlist(strsplit(y[grepl("email = ", y)], "email ="))[2])
} else {
    z <- file.path(Sys.getenv("HOME"), ".gitconfig")
    if (file.exists(z)) {
        email <- Trim(unlist(strsplit(y[grepl("email = ", y)], "email ="))[2])
    } else {
        warning(paste("Set `email` in", x))
    }
}
cmdEM <- paste0(shQuote(gitpath), sprintf(" config --global user.email %s", email))        
system(cmdEM, intern = T)

## Initial commit
cmd4 <- paste0(shQuote(gitpath), ' commit -m "Initial commit"')  
system(cmd4, intern = T) 

## establish connection between local and remote
cmd5 <- paste0(shQuote(gitpath), " remote add origin https://github.com/",
    github.user, "/", repo, ".git")  
shell(cmd5, intern = T) 

## push local to remote 
cmd6 <- paste0(shQuote(gitpath), " push -u origin master")  
shell(cmd6, intern = T) 

setwd(wd)

我知道脚本有点长,但重新创建问题并复制问题是必要的:

Note我根据西蒙的回答更新了问题,因为他是正确的并且更接近推动。原问题的内容可以找到here https://copy.com/SDmx8irgIEGpoXTP.


如果使用 https 地址,请确保:

  • 环境变量%HOME%被定义为
  • a _netrc文件中存在并具有正确的凭据,可以推送回您的存储库

该文件应包含:

machine github.com
login username
password xxxx
protocol https

即使你有,这也有效最近在GitHub上激活了双因素身份验证 https://stackoverflow.com/a/18607931/6309.

那么你的推送就不会超时:

cmd6 <- paste0(shQuote(gitpath), " push -u origin master")  
shell(cmd6, intern = T) 

这比设置 ssh 公钥/私钥 https://stackoverflow.com/a/18679161/6309.


As the OP 泰勒·林克 https://stackoverflow.com/users/1000343/tyler-rinker 评论了 https://stackoverflow.com/questions/18648737/use-r-to-push-local-repo-to-github-on-windows#comment27565367_18692400, 环境%HOME%我的另一个答案中有说明“Git - 如何使用.netrcWindows 上的文件用于保存用户和密码 https://stackoverflow.com/a/6031266/6309".
这通常是由git-cmd.bat https://github.com/msysgit/msysgit/blob/master/git-cmd.bat:

if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@if not exist "%HOME%" @set HOME=%USERPROFILE%

但您也可以手动完成。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Windows上使用R将本地repo推送到github 的相关文章

随机推荐

  • 如何绘制参数的不同值的分布?

    我有以下脚本 library ggplot2 values lt c 0 1 0 15 0 2 0 3 0 5 1 1 5 2 2 5 colours lt palette 1 length values p lt ggplot data
  • 如何在 UipageViewController 中快速预加载下一个和上一个视图

    由于经过大量搜索和 RND 以及许多不同的代码实现后 滑动延迟问题尚未解决 因此我猜预加载下一个和上一个视图控制器将解决我的滑动延迟问题 如何在滑动之前加载下一个和上一个视图 经过长时间的 rnd 和搜索后没有找到解决方案 在显示可见控制器
  • 如何添加 favicon gatsby-config.js?

    所以我尝试在我的博客中添加图标 代码如下 在我的 gatsby config js 中 module exports siteMetadata title Chatbiz Blog plugins gatsby plugin react h
  • 是否有任何解决方法可以禁用 Google 地球插件的“缩放时自动倾斜”功能?

    最新版本的 Google 地球附带了一项名为 缩放时自动倾斜 的功能 如果启用 当您放大到靠近地面时 Google 地球会自动将相机向地平线倾斜 可以从 GUI 中禁用此功能 首选项 gt 导航选项卡 不幸的是 到目前为止我还没有找到一种方
  • 使用 PowerShell 将文件上传到 SFTP

    我们被要求设置从我们的一台服务器到 SFTP 站点的自动上传 每个星期一早上都会有一个文件从数据库导出到文件管理器 他们希望在星期二将该文件上传到 SFTP 我们当前使用的身份验证方法是用户名和密码 我相信也可以选择拥有密钥文件 但选择了用
  • 如何在 MongoDB 查询中使用正则表达式变量

    我想根据我构造的正则表达式查询 MongoDB 文档 例如 我构建了一个简单的正则表达式 如下所示 它是 Nodejs 中随机字母和随机数字的组合 var randnum Math floor Math random 10 1 var al
  • DropDownList 的行为不符合预期

    我的 DropDownListFor 遇到问题 希望您能帮助我 我猜这是你要么知道要么不知道的事情之一 问题是我的数据库中有一个国家 地区表 其中包含国家 地区列表 我希望从下拉列表中获得的行为是在地址表中创建一个外键引用 指向下拉列表中选
  • 使用异步而不等待?

    考虑使用异步而不等待 https stackoverflow com questions 12016567 using async without await 认为您可能误解了 async 的作用 警告是 完全正确 如果您将方法标记为异步但
  • Scala 中的列表和元组

    来自 Martin Odersky 的 Scala 编程 一书 另一个有用的容器对象是元组 像列表一样 元组是不可变的 但与列表不同的是 元组可以包含不同类型的元素 但我可以拥有 val oneTwoThreee List 1 2 Thir
  • 在 Bash 中捕获 stdout 和 stderr [重复]

    这个问题在这里已经有答案了 我知道这个语法 var myscript sh or var myscript sh 将捕获结果 stdout of myscript sh into var 我可以重定向stderr into stdout如果
  • Oracle 数据库中的 sysdate 和 dbtimezone 不同

    通过这个查询 select sysdate from dual 结果是 27 09 2018 07 50 50 这UK time with Select dbtimezone from dual output 10 00 我想要sysdat
  • 如何从 LogisticRegressionCV 和 GridSearchCV 获得可比较且可重复的结果

    我想用不同的参数对不同的分类器进行评分 为了加速LogisticRegression I use LogisticRegressionCV 至少快 2 倍 并计划使用GridSearchCV为他人 但问题是它给了我平等C参数 但不是AUC
  • 为什么我的 CSS 样式没有被应用?

    我有这个html p span class fancify Parting is such sweet sorrow span span Bill Rattleandrollspeer span p 以及这个 css 添加到 Site cs
  • 我如何从库比蒂诺日期选择器中隐藏一天

    我只需要从日期选择器中获取年份和月份 那么如何从日期选择器中隐藏日期 CupertinoDatePicker initialDateTime DateTime now onDateTimeChanged DateTime newdate p
  • 使用 mapbox-gl-js 集群自定义 html 标记

    我正在使用 mapbox gl js API 并将其与 React 一起使用来创建一些自定义标记 如下所示 let div document createElement div let marker new mapboxgl Marker
  • 在 R 中绘制相关矩阵,如 Excel 示例所示

    我一直在尝试尽量减少对 Excel 的使用 转而使用 R 但在显示简单的数据单元格时仍然遇到困难 而这通常是分析的最后一步所需要的 以下示例是我想要破解的示例 因为它将帮助我切换到 R 来完成工作流程的这一关键部分 我想说明 R 中的以下相
  • Android 上的 WiFi Direct 无法正常工作

    我正在尝试在 android Jelly Bean 4 1 1 中直接使用 wifi 开发一个应用程序 如果启用了 p2p 我会立即致电 mManager discoverPeers mChannel actionListener 之后我会
  • Matplotlib:将颜色条添加到不可映射的对象

    我有一系列代表变量变化的线 每个都有独特的颜色 因此 我想在绘图旁边添加一个颜色条 所需的输出如下所示 问题是plot是不可映射的对象 即必须手动添加颜色条 我认为我当前的解决方案 如下 不是最佳的 因为它涉及我没有兴趣控制的尺寸参数 我更
  • 有没有一种简单的方法可以在 matplotlib 中为滚动垂直线设置动画?

    我想要一个我所描述的进度标记 它在音频播放实用程序中似乎很常见 我认为在 matplotlib 中这相当于左 右动画plt vlines 我的代码采用 2 秒的数据数组并创建音频时间序列可视化 我正在努力创建一条动画垂直线 该线会在绘图上从
  • Windows上使用R将本地repo推送到github

    我曾经问过一个很类似的问题 https stackoverflow com questions 14912161 repo from rstudio to github并得到了从命令行有效的响应 但我现在想使用 R 从 Windows 自动