Question:
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?
Answer:
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:
1 2 |
git checkout -- path/to/folder |
If the deletion is already indexed, you should reset that first:
1 2 3 |
git reset -- path/to/folder git checkout -- path/to/folder |
Restore the full working tree (not a single folder), but lose all uncommitted changes
1 2 |
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.
1 2 |
git rev-list -n 1 HEAD -- |
Then checkout the version at the commit before, using the caret (^
) symbol:
1 2 |
git checkout |
Restore the full working tree from a distant commit
1 2 |
git reset --hard |