Restore a deleted folder in a Git repo

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> 

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

The only thing that worked for me was to checkout the repo in another folder. Assume the current repo is in /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

I can now go to /home/me/temp and do whatever I want. For example

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

链接地址: http://www.djcxy.com/p/18946.html

上一篇: 如何从git仓库的历史记录中恢复整个目录?

下一篇: 恢复Git仓库中已删除的文件夹