在 Git 存储库中恢复已删除的文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30875205/
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 a deleted folder in a Git repo
提问by json2021
I have deleted all the contents inside a folder and the folder is empty. I still had a copy in my remote repo. But when I did a git pull
it didn't put back the deleted files isn't is supposed to do that?
我已删除文件夹中的所有内容,该文件夹为空。我的远程仓库中还有一份副本。但是当我做了一个git pull
它没有放回被删除的文件不是应该这样做吗?
So I did some research and saw that you can revert a file by doing
git checkout <revision> -- <name of file>
所以我做了一些研究,发现你可以通过做来恢复文件
git checkout <revision> -- <name of file>
But that only works on files.
但这仅适用于文件。
How can I retrieve all the files inside the directory?
如何检索目录中的所有文件?
回答by Nick Volynkin
Everything you can do with a file, you can do with a folder too.
您可以对文件执行的所有操作,也可以对文件夹执行。
Also note Find and restore a deleted file in a Git repository
Files are deleted from working tree but not committed yet:
文件已从工作树中删除但尚未提交:
If you have not yet indexed (git add
) your changes you can revert content of a directory:
如果您尚未索引 ( git add
) 您的更改,您可以恢复目录的内容:
git checkout -- path/to/folder
If the deletion is already indexed, you should reset that first:
如果删除已被索引,您应该先重置它:
git reset -- path/to/folder
git checkout -- path/to/folder
Restore the full working tree (not a single folder), but lose all uncommitted changes
恢复完整的工作树(不是单个文件夹),但丢失所有未提交的更改
git reset --hard HEAD
When files are deleted in some commit in the past:
当文件在过去的某些提交中被删除时:
Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.
查找影响给定路径的最后一次提交。由于该文件不在 HEAD 提交中,因此此提交必须已将其删除。
git rev-list -n 1 HEAD -- <file_path>
Then checkout the version at the commit before, using the caret (^
) symbol:
然后使用插入符号 ( ^
) 符号检查之前提交时的版本:
git checkout <deleting_commit>^ -- <file_path>
Restore the full working tree from a distant commit
从远程提交恢复完整的工作树
git reset --hard <revision>
回答by Ivan Mushketyk
If you have not yet commited your changes you can revert content or a directory:
如果您尚未提交更改,则可以还原内容或目录:
git checkout -- removed_directory
If you want to revert all changes do:
如果要还原所有更改,请执行以下操作:
git reset --hard HEAD
回答by gman
The only thing that worked for me was to checkout the repo in another folder. Assume the current repo is in /home/me/current
.
唯一对我有用的是在另一个文件夹中签出 repo。假设当前的 repo 在/home/me/current
.
I then did
然后我做了
git clone /home/me/current /home/me/temp
This make a separate clone of the repo in /home/me/temp
这使 repo 的单独克隆在 /home/me/temp
I can now go to /home/me/temp
and do whatever I want. For example
我现在可以/home/me/temp
去做任何我想做的事。例如
git reset --hard commit-hash-before-delete
Now I can copy the deleted file folder back
现在我可以将删除的文件夹复制回来
cp -r /home/me/temp/some/deleted/folder /home/me/current/some/deleted/folder
And delete the temp folder
并删除临时文件夹
rm -rf /home/me/temp
The examples of
的例子
git checkout -- some/deleted/folder
git checkout -- some/deleted/folder/*
DO NOT WORK
不工作
$ git checkout -- some/deleted/folder/*
zsh: no matches found: some/deleted/folder/*
$ git checkout -- some/deleted/folder
error: pathspec 'some/deleted/folder' did not match any file(s) known to git.
Other examples like
其他例子如
git reset --hard HEAD
are destructive beyond just the deleted files. Any other changes will also be lost.
破坏性不仅仅是删除的文件。任何其他更改也将丢失。
Similarly
相似地
git reset --hard some-commit
will lose any commits after some-commit
之后将丢失任何提交 some-commit
回答by iethree
As of git 2.24.0, there's an experimental new git command: git restore
从 git 2.24.0 开始,有一个实验性的新 git 命令:git restore
git restore --staged some/deleted/folder
回答by jBelanger
You can restore files or folder with git restore.
您可以使用 git restore 恢复文件或文件夹。
git restore --source master~1 "PATH_IN_YOUR_REPO"
Here, master~1reverts your folder to "1" revision back from your master branch.
在这里, master~ 1将您的文件夹从您的主分支恢复为“1”修订版。
回答by lostphilosopher
If you don't specify a specific file you should be able to pull the full contents of a specific commit. Like: git checkout 264794319e9695ba843cd6
(assuming that hash has all your files at the right state).
如果您不指定特定文件,您应该能够提取特定提交的完整内容。像:(git checkout 264794319e9695ba843cd6
假设哈希使您的所有文件都处于正确状态)。
The reason pull
isn't restoring files is that git sees your deletions as the more recent change, applying that on top of whatever you're pulling.
pull
不恢复文件的原因是 git 将您的删除视为最近的更改,将其应用到您正在拉取的任何内容之上。
(I'd recommend experimenting in a new branch.)
(我建议在新分支中进行试验。)
回答by justadev
for uncommited deletions, Its as simple as this :
对于未提交的删除,就像这样简单:
git reset HEAD rel/path/to/deleted/directory/*
git reset HEAD rel/path/to/deleted/directory/*