【问题标题】:How do I revert to a previous git in R studio?如何在 R studio 中恢复到以前的 git?
【发布时间】:2016-11-22 17:33:55
【问题描述】:

我知道类似的问题已经在一堆帖子中得到解决,但我还是不知道如何解决这个问题。

所以,我已经用这个 git repo 同步了我的本地目录

Dario (master *) replicationKumar $ git remote -v
origin  git@github.com:DarioBoh/replicationKumar.git (fetch)
origin  git@github.com:DarioBoh/replicationKumar.git (push)

我成功推送了一些提交,所以我很有信心我已经正确同步了所有内容。 还有,我跑了

Dario (master *) replicationKumar $ git pull origin master
From github.com:DarioBoh/replicationKumar
 * branch            master     -> FETCH_HEAD
Already up-to-date.

这表明我应该复制我在 github 上的内容。 自上次提交以来,我删除了一些文件,现在我想从这次提交的远程目录中恢复所有文件

Dario (master *) replicationKumar $ git log
commit 91a3dfdb30084654a7a5517250d2a70dfddb931b
Author: DarioBoh <bonarettidario@gmail.com>
Date:   Tue Jul 19 10:00:12 2016 -0500

    REFACTOR linechart moved from server to chart.R

其实本地目录目前是这样的

Dario (master *) replicationKumar $ ls -a
.           .RData          .Rproj.user     .gitignore
..          .Rhistory       .git            replicationKumar.Rproj

我以为 checkout 会完成这项工作,我做到了

Dario (master *) replicationKumar $ git checkout 91a3dfdb30084654a7a5517250d2a70dfddb931b
D   charts.R
D   datasets.R
D   server.R
D   sidebarMenu.R
D   ui.R
D   ui1.R
D   ui2.R
Note: checking out '91a3dfdb30084654a7a5517250d2a70dfddb931b'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 91a3dfd... REFACTOR linechart moved from server to chart.R

但是,我的本地目录并没有像我预期的那样显示检出提交中存在的文件

Dario ((91a3dfd...) *) replicationKumar $ ls -a
.           .Rhistory       .gitignore
..          .Rproj.user     FETCH_HEAD
.RData          .git            replicationKumar.Rproj

【问题讨论】:

    标签: r git github rstudio


    【解决方案1】:

    如果您只想恢复这些文件,并在 master 分支上执行此操作,那么您应该:

    • 查看主分支
    • 只签出上一次提交中的那些文件
    • 暂存更改(还原的文件)
    • 进行新的提交

    这将如下所示:

    git checkout master
    git checkout [commit hash] [path/to/file.txt]
    ...
    git add .
    git status (does everything look right?)
    git commit
    

    这将向 master 添加一个新的提交,它将文件从指定的旧提交恢复到它们的状态。之后你可能会想运行另一个git push

    【讨论】:

    • 我对 git 很陌生,所以也许我误解了它的工作方式:我认为可以简单地回到以前的提交,但你说我应该再次检查这些文件.因此,基本上我不能简单地退后一步,对吗?
    • 您可以返回之前的提交,但您将丢失自该提交以来所做的任何其他更改(即不仅仅是那些文件的更改)。这是可取的吗?
    • 如果是这样,您需要使主分支指针指向较旧的提交。命令很危险,会丢弃您在工作目录中所做的所有更改 git reset --hard [commit hash]。自指定提交以来您所做的提交仍将在 repo 中,但将不再是 master 分支的一部分。
    • @Dambo - 话虽如此,如我的回答所示,继续前进要安全得多。没有奇怪的副作用,只是一个将文件更改为旧状态的新提交。
    • 好吧,我有提交 1,2,3,4。我现在处于提交 1,最后一个。我想看到我的文件好像我在提交 2 中。相反,您说要查看所有内容,就好像我在 2 中一样,我应该删除 1 或将 2 提交到新的提交中'0'。那么没有直接的方法去提交 2 吗?
    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2011-06-27
    • 2011-05-06
    相关资源
    最近更新 更多