r - 使用 GIT2r 检查提交时出现问题
问题描述
据我从git2r
文档中可以看出,只有两种方法可以检索以前的提交:签出和重置。
我遇到的问题是,在这两种情况下,我似乎都丢失了所有较新的提交。也许我不明白这里发生了什么?它的意思是那样工作吗?
我只在本地使用它,所以没有从任何地方推或拉,只是本地提交。
这些是我正在使用的命令:
# to create a new commit
repo <- repository(path = "/blah/blah/blah")
add(repo,"*")
commit(repo,"my new nice commit")
# to retrieve a previous commit:
checkout(commits(repo)[[2]]) # if only a single previous commit exists it will be number 2
OR
reset(commits(repo)[[2]])
再次两者都导致丢失较新的提交。有谁知道发生了什么。
非常感谢您!
解决方案
有几种方法可以解决这个问题。首先,我将演示一个示例存储库的创建,以便您可以准确地重现它:
library(git2r)
path <- "SOanswer"
dir.create(path)
repo <- init(path)
writeLines("Commit1", con = file.path(path, "commit1.txt"))
add(repo, "commit1.txt")
commit(repo, "First commit message")
repository_head(repo)
commits(repo)
writeLines("Commit2", con = file.path(path, "commit2.txt"))
add(repo, "commit2.txt")
commit(repo, "Second commit message")
现在您的问题是,如果您运行checkout(commits(repo)[[2]])
,您将丢失提交 2,并且它将不再显示在commits()
. 但是,您可以只执行以下操作git checkout master
(有关在普通 git 上下文中讨论类似问题,请参见例如,this question):
list.files(path)
# [1] "commit1.txt" "commit2.txt"
checkout(commits(repo)[[2]])
list.files(path)
# [1] "commit1.txt"
checkout(repo, branch = "master")
list.files(path)
# [1] "commit1.txt" "commit2.txt"
这会将您带到主分支的 HEAD。但是,假设您想进行特定的提交。您可以使用提交 SHA 来做到这一点。这是一个例子:
writeLines("Commit3", con = file.path(path, "commit3.txt"))
add(repo, "commit3.txt")
commit(repo, "Third commit message")
completed_commits <- commits(repo) # Store the commits so we know the SHAs
list.files(path)
# [1] "commit1.txt" "commit2.txt" "commit3.txt"
checkout(completed_commits[[3]])
list.files(path)
# [1] "commit1.txt"
checkout(completed_commits[[2]])
list.files(path)
# [1] "commit1.txt" "commit2.txt"
checkout(completed_commits[[1]])
list.files(path)
# [1] "commit1.txt" "commit2.txt" "commit3.txt"
推荐阅读
- database - ErrorException (E_NOTICE) 未定义的偏移量:0 laravel
- javascript - Javascript 动画不流畅?
- android - 领域崩溃项目已删除。但我确实先重新查询领域
- python - 有没有办法知道函数中传递了多少参数?
- sql-server - 使用 SQL Server 中的 Excel VBA 在新行中显示数据
- r - 通过使用 R 中的 PortfolioAnalytics 包最大化夏普比率来解决最佳权重
- node.js - Firebase 和云服务(亚马逊、Bluehost)有什么区别?
- date - SSRS 日期参数 - 外语问题
- maven - Maven 将 jar 库留空
- python - 从 Django Query 中提取值的正确方法