恢复删除的文件不在git中登台 [英] Restore deleted file not staged in git

查看:151
本文介绍了恢复删除的文件不在git中登台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不小心删除了我的源代码的整个目录......用一个漂亮的rm -r。我知道,真的很糟糕;但幸运的是,我在包含目录中有一个git repo。因此,git拥有一大堆已删除文件的 unstaged 更改。例如:

 deleted:src / caronmonitor / server.py

如何恢复这些文件?网上有建议要做:

  git结帐文件

  git revert< commit> 

但据我所知,在上次提交时会将文件恢复到它的状态。我不想回到最后一次提交,而是在删除操作之前回到正确的位置。我可以在gitk中查看并查看我的文件,就像它们在删除之前一样;因此这一定是可能的。

解决方案

不,GIT 没有任何魔力。如果你没有上演或承诺你的改变,那么这些都没有了。
您可以看到删除内容,但只能将这些内容还原为您告诉GIT记住它的最后一个状态。



您必须明确告知GIT通过登台和提交来记住你的变化。

所以,如果你的资料​​库中有file.txt文件的内容:

  int main(argc [] args){
System.out.println(example);
}

这是您提交或上传的最后一项变更。



现在编辑file.txt以包含如下内容:

  int main(argc [ ] args){
System.out.println(example);
System.out.println(嘿,我可以打印更多的行);
}

您保存文件,关闭编辑器,对git不做任何操作, c $ c> rm -r
现在这个文件就在那里,git引用了文件,它被删除了,但是这个文件在git中的内容只有:

  int main(argc [] args){
System.out.println(example);
}


I accidentally removed the entire directory of my source code...with a nice rm -r. I know, really bad; but fortunately, I had a git repo in the containing directory. Thus, git has a huge list of unstaged changes of deleted files. For example:

"deleted:   src/caronmonitor/server.py" 

How do I get these files back? There is advice all over the web to do:

git checkout file

or

git revert <commit>

But as I understand that will restore the file to it's state at the last commit. I don't want to go back to the last commit but instead go back to right before the delete operation. I can look in the gitk and see my files as they were before the delete; thus this must be possible.

解决方案

Not, GIT does not do any magic. If you did not staged or commited your changes then those are gone. You can see deletions but you can only revert those to last state that you told GIT to remember it for you.

You have to tell explicitly GIT to remember your changes by staging and commiting.

So if you have file.txt in your repository with content:

    int main(argc[] args){
      System.out.println("example");
    }

This was your last change that was commited or staged.

Now you edit file.txt to contain something like:

int main(argc[] args){
 System.out.println("example");
 System.out.println("Hey I can print more lines");
}

You save your file, close editor, do nothing with git and do rm -r Now the file is there, and git has reference to file and that it was deleted but content of this file in git is only:

    int main(argc[] args){
      System.out.println("example");
    }

这篇关于恢复删除的文件不在git中登台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆