如何在Git中恢复旧的提交?

时间:2011-04-21 07:53:41

标签: git git-branch

我在这个问题上非常糟糕。 我正在开发一个分支'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 status显示:

 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 status返回:

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")

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

只需阅读您引用的git status输出,就好像您可以恢复已删除的文件,例如:

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

...当你在feature1-branch时。