恢复Git仓库中已删除的文件夹 [英] Restore a deleted folder in a Git repo
问题描述
我删除了文件夹内的所有内容,并且该文件夹为空。我在我的远程回购中仍然有一份副本。但是当我做了一个 git pull
它没有放回被删除的文件不是应该这样做的?
因此,我做了一些研究,看到您可以通过执行
git checkout< revision>来恢复文件。 - <文件名称>
但这只适用于文件。
如何检索目录中的所有文件?
你可以用文件做的所有事情,你也可以使用一个文件夹。
文件从工作树中删除,但尚未提交:
如果您还没有索引( git add
)你可以恢复目录的内容:
git checkout - 路径/到/文件夹
如果删除已经编入索引,您应该先重置:
git reset - path / to / folder
git checkout - path / to / folder
恢复完整的工作树(不是单个文件夹),但会丢失所有未提交的更改
git reset --hard HEAD
过去在某些提交中删除文件时:
查找最后一次影响给定路径的提交。由于该文件不在HEAD提交中,因此该提交必须将其删除。
git rev-list -n 1 HEAD - < file_path>
然后使用脱字符( ^
)符号:
git checkout< deleting_commit> ^ - < file_path>
从远处提交完整工作树
git reset --hard< revision>
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?
So I did some research and saw that you can revert a file by doing
git checkout <revision> -- <name of file>
But that only works on files.
How can I retrieve all the files inside the directory?
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 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.
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>
这篇关于恢复Git仓库中已删除的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!