恢复未在 git 中暂存的已删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24911809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Restore deleted file not staged in git
提问by Vance T
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 unstagedchanges of deleted files. For example:
我不小心删除了我的源代码的整个目录......用一个很好的 rm -r。我知道,真的很糟糕;但幸运的是,我在包含目录中有一个 git repo。因此,Git有一个巨大的名单不分阶段删除的文件的变化。例如:
"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.
但据我所知,这会将文件恢复到上次提交时的状态。我不想回到上次提交,而是回到删除操作之前。我可以查看 gitk 并查看删除之前的文件;因此这一定是可能的。
采纳答案by Mateusz
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.
不,GIT不会做任何魔法。如果您没有上演或提交您的更改,那么这些都将消失。您可以看到删除,但您只能将它们恢复到您告诉 GIT 为您记住它的最后状态。
You have to tell explicitly GIT to remember your changes by staging and commiting.
您必须明确告诉 GIT 通过暂存和提交来记住您的更改。
So if you have file.txt in your repository with content:
因此,如果您的存储库中有包含内容的 file.txt:
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:
现在您编辑 file.txt 以包含以下内容:
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:
您保存文件,关闭编辑器,对 git 不做任何操作,然后执行rm -r
现在文件在那里,并且 git 引用了文件并且它已被删除,但该文件在 git 中的内容仅为:
int main(argc[] args){
System.out.println("example");
}
回答by u1860929
Yes, You should do a git checkout filename
是的,你应该做一个 git checkout filename
If you have multiple files, and want to restore all of them back, use git checkout .
from the root of your git directory.
如果您有多个文件,并希望将所有文件恢复回来,请git checkout .
从 git 目录的根目录使用。
A checkout will restore the files to last version that was added/committed; if a file hadn't been added or committed yet, than it will be lost.
签出会将文件恢复到添加/提交的最新版本;如果一个文件还没有被添加或提交,那么它就会丢失。
So, for example:
因此,例如:
$ git init && touch test1.txt test2.txt test3.txt
$ git add test1.txt && git commit -m "test1" && git add test2.txt
$ ls -a
. .. .git test1.txt test2.txt test3.txt
#deleting files below, 2 will be recovered, 3rd will be gone.
$ rm *
$ ls -a
. .. .git
$ git checkout .
$ ls -a
. .. .git test1.txt test2.txt
#test3.txt is gone, test2.txt was recovered, even though not committed but just added
回答by akgill
git won't have stored any changes that were never added or committed. The best you can do is git checkout that deleted directory, and you'll get it back in the state it was in when things were last committed.
git 不会存储任何从未添加或提交的更改。您能做的最好的事情是 git checkout 删除的目录,您将把它恢复到上次提交时的状态。
git checkout <branch_you_are_in> <directory_of_all_your_source_code>
git checkout <branch_you_are_in> <directory_of_all_your_source_code>
The local uncommitted changes you are seeing is probably the uncommitted deletion, i.e. git sees that you've deleted a bunch of files, but you haven't committed the fact that they are deleted yet, so git says 'hey, there are some changes (in this case, deletions) you haven't committed.'
您看到的本地未提交的更改可能是未提交的删除,即 git 看到您删除了一堆文件,但您还没有提交它们已被删除的事实,因此 git 说'嘿,有一些更改(在这种情况下,删除)你还没有提交。
回答by Hawklike
Something similar has happened to me. I have used the Android Studio, but it would work in other JetBrainsIDE's as well.
类似的事情也发生在我身上。我使用过Android Studio,但它也适用于其他JetBrainsIDE。
I have restored my deleted files via Local history > show history
and then I have reverted all deleted files.
我已经恢复了我删除的文件Local history > show history
,然后我恢复了所有已删除的文件。
Hope it helps someone.
希望它可以帮助某人。