【问题标题】:How do I revert back to an older commit in Git?如何在 Git 中恢复到较旧的提交?
【发布时间】:2011-04-21 07:53:41
【问题描述】:

我在这件事上搞砸了。 我正在处理一个分支“feature1-branch”并提交了更改,但没有将它们与 master 合并。

git 日志显示如下内容:

git log
commit 0829f9f0b68ce58ca37efd4e07cf3e8b3b67ed49
Date:   Thu Apr 21 00:38:58 2011 -0700

feature1-branch lots and lots of changes

我不确定我在想什么,但我确实做到了:

git checkout master

这删除了一堆使用分支“feature1-branch”添加的新文件。

现在 git 状态显示:

 git status
 # On branch feature1-branch
 # Changed but not updated:
 #   (use "git add/rm <file>..." to update what will be committed)
 #   (use "git checkout -- <file>..." to discard changes in working directory)
 #
 #  deleted:    app/controllers/activations_controller.rb
 #  deleted:    app/controllers/settings_controller.rb
 #  modified:   app/controllers/application_controller.rb
 #  deleted:    app/controllers/products_controller.rb

提到的已删除文件不再存在。 我如何让他们回到我提交时的状态

0829f9f0b68ce58ca37efd4e07cf3e8b3b67ed49

我担心这些文件是否永远消失了。

感谢您的帮助。

添加附加信息:

git log -1 feature1-branch
commit 0829f9f0b68ce58ca37efd4e07cf3e8b3b67ed49 
Date: Thu Apr 21 00:38:58 2011 -0700 
feature1-branch lots and lots of changes

这是 git checkout 返回的内容:

git checkout feature1-branch 
D app/controllers/activations_controller.rb 
D   app/controllers/products_controller.rb 
M app/controllers/application_controller.rb

git 状态返回:

git status 
# On branch feature1-branch 
# Changed but not updated: 
# (use "git add/rm <file>..." to update what will be committed) 
# (use "git checkout -- <file>..." to discard changes in working directory) 
# # deleted: app/controllers/activations_controller.rb 
# deleted: app/controllers/products_controller.rb 

no changes added to commit (use "git add" and/or "git commit -a")

【问题讨论】:

    标签: git git-branch


    【解决方案1】:

    我不明白 git status 中说您在 feature1 分支上的那一行。但是,您可以这样做并粘贴输出吗? git log -1 feature1-branch?如果它告诉您它指向0829f9f0b68ce58ca37efd4e07cf3e8b3b67ed49,您只需git checkout feature1-branch 即可到达您切换到master 之前的位置。

    【讨论】:

    • git log -1 feature1-branch commit 0829f9f0b68ce58ca37efd4e07cf3e8b3b67ed49 Date: Thu Apr 21 00:38:58 2011 -0700 feature1-branch lots and lots of changes
    • 等等。 pivot_cars_apts 是什么?我还以为叫feature1-branch?
    • 这是 git checkout 返回的 git checkout feature1-branch D app/controllers/activations_controller.rb D app/controllers/products_controller.rb M app/controllers/application_controller.rb
    • 我看到提交和你上次提交的一样。你在checkout 之后把文件找回来了吗?
    • 不,文件已经不存在了。它说我已经在“feature1-branch”上,并显示了一堆已删除的文件。 git status # On branch feature1-branch # Changed but not updated: # (use "git add/rm &lt;file&gt;..." to update what will be committed) # (use "git checkout -- &lt;file&gt;..." to discard changes in working directory) # # deleted: app/controllers/activations_controller.rb # deleted: app/controllers/products_controller.rb no changes added to commit (use "git add" and/or "git commit -a")
    【解决方案2】:

    我怀疑发生的事情是您在某个时候删除了这些文件,但没有提交删除。这意味着当您切换分支时,它们仍将显示为已删除,因为 git 会在您更改分支时尝试保留工作树中的修改,除非这会丢失数据或产生问题。

    只需阅读您引用的git status 的输出,您就可以通过以下方式恢复已删除的文件,例如:

    git checkout -- app/controllers/activations_controller.rb app/controllers/settings_controller.rb
    

    ...当你在feature1-branch时。

    【讨论】:

    • 谢谢马克。做到了。当我意识到发生了什么时,我不小心运行了 git checkout master 并按了 ctrl-c。这把整个事情搞砸了。你和努法尔都帮了大忙,要是我能给你们买一两杯啤酒就好了:)
    • @truthSeekr:没问题 :) 如果您在没有足够磁盘空间的情况下尝试git checkout &lt;branch-name&gt;,可能会陷入类似的混乱状态...
    猜你喜欢
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2015-02-07
    • 2020-06-19
    • 2011-05-23
    • 2018-10-17
    • 2015-10-08
    • 2021-10-06
    相关资源
    最近更新 更多