使用r比较git中文件的两个版本的两个数据



我构建了一个数据集,希望将其置于版本控制之下。作为发布过程的一部分,我想比较数据集的不同版本。为了简单起见,我想将数据保持为csv格式。

如何使用R获取一个数据文件的两个不同的git版本?(下一步是比较内容,但这不是问题的一部分)

示例代码:(编辑2017-11-19修复了一些错误)

#
# re-using git2r sample code for status
#
## Create a temporary git repository
path <- tempfile(pattern="git2r-")
dir.create(path)
repo <- init(path)
setwd(path)
#
# set up a simple frame and commit it twice 
#
df <- data.frame(x = 1, y = 1:2)
write.csv2(df, "df.csv", row.names = FALSE)
add(repo, "df.csv")
commit(repo, "First commit message")
df <- data.frame(x = 1, y = 1:3)
write.csv2(df, "df.csv", row.names = FALSE)
add(repo, "df.csv")
commit(repo, "2nd commit message")

我正在寻找的-一种恢复文件特定版本的方法

df_first_commit <- 
df_2nd_commit <- 

并使用接受答案的帮助

checkout(commits(repo)[[1]])
df_2nd_commit <- read.csv2("df.csv")
checkout(commits(repo)[[2]])
df_first_commit <- read.csv2("df.csv")

我找到了一种方法,也许还有更好的方法。它将涉及不同版本的CCD_ 1,为了安全地做到这一点,重要的是从干净的状态开始,没有未提交的编辑。

首先,你需要找到你想切换到的提交,检查结果:

commits(repo)

一旦你知道你感兴趣的提交,就切换到它:

# n is the commit number to switch to, 1 is the last, 2 is the one before, ...
checkout(commits(repo)[[n]])

此时,您可以将文件读取到df_foo中。您可以切换到另一个提交以读取df_bar进行比较。要切换回以前的状态:

checkout(repo, branch="master")

基于CCD_ 4,我希望能够在给定的修订版签出特定的文件,但这对我不起作用:

# doesn't work
checkout(commit, path="df.csv")

特定文件的签出似乎只适用于repo参数,而不适用于特定的提交。例如,这可以替换索引中特定文件的内容:

checkout(repo, path="df.csv")

更重要的是,我在寻找git show SHA:path的等价物,获取文件的内容,但show方法的文档是无用的,我微弱的尝试也没有结果:

# nothing useful here
show(commit, ":df.csv")
show(paste0(commit@sha, ":df.csv"))

最新更新