git 从存储库中恢复已删除的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11198542/
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 from repository
提问by Eugen
I'm being in a transition from SVN to GIT and got a question for which I cannot find an answer. I'll describe am usual scenario when I work with some open source projects via SVN.
我正在从 SVN 过渡到 GIT,遇到了一个我找不到答案的问题。我将描述当我通过 SVN 处理一些开源项目时的常见场景。
- make a checkout
- start messing around with files, make changes, to test how the project works.
- after p.2 some files are heavily modified and I have no chance to get back to original state.
- I delete the modified files from disk "rm filename.cpp", run the command "svn update" and voila, all original files are back.
- 结帐
- 开始处理文件,进行更改,以测试项目的工作方式。
- 在 p.2 之后,一些文件被大量修改,我没有机会恢复到原始状态。
- 我从磁盘“rm filename.cpp”中删除修改后的文件,运行命令“svn update”,瞧,所有原始文件都回来了。
All this works fine with GIT as well, except p.4. I try to make "git pull" it says that project is up to date and I don't get the original files even though they are missing from local folder.
除了第 4 页之外,所有这些都适用于 GIT。我尝试制作“git pull”,它表示该项目是最新的,即使本地文件夹中缺少原始文件,我也没有获得原始文件。
What is the correct command for p.4 when working with git. Thx
使用 git 时,第 4 页的正确命令是什么?谢谢
采纳答案by poke
Try to checkout the current branch (or HEAD
):
尝试签出当前分支(或HEAD
):
git checkout HEAD
Or if you want to revert everythingto the last commited state (warning:this will permanently delete all uncommited changes!), you can also reset hard:
或者,如果您想将所有内容恢复到上次提交的状态(警告:这将永久删除所有未提交的更改!),您也可以硬重置:
git reset --hard
回答by madth3
The simplest way is indicated in the output of git status
:
最简单的方法显示在输出中git status
:
git checkout -- file
where file can be the name of a deleted file. This will recover the deleted file without affecting other files.
其中 file 可以是已删除文件的名称。这将恢复已删除的文件,而不会影响其他文件。
回答by valenz
This is what I would do:
这就是我会做的:
git reset HEAD <file-path>
git checkout -- <file-path>
git reset HEAD <file-path>
git checkout -- <file-path>
回答by OppfinnarJocke
Git is awful in this case, and I have been struggling with it so much that it is incredible. Here is the way I do it:
在这种情况下,Git 很糟糕,我一直在为它苦苦挣扎,以至于难以置信。这是我的做法:
- Clone the remote repo to some place on your local HD different from where you have your existing local repo
- Copy the deleted files/directories from the newly cloned repo to the existing local repo
- Commit the changes to your existing local repo, if needed push also to the remote repo
- Delete the newly cloned local repo
- 将远程存储库克隆到本地 HD 上与现有本地存储库不同的某个位置
- 将删除的文件/目录从新克隆的 repo 复制到现有的本地 repo
- 将更改提交到您现有的本地存储库,如果需要也推送到远程存储库
- 删除新克隆的本地仓库
Why it should be this complicated and time consuming to get such a simple task done escapes me...
为什么完成如此简单的任务应该如此复杂和耗时,这让我无法理解......