How to restore a whole directory from history of git repository?
try adding '--' between revisions and paths:
git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 -- path/to/the/folder/
And if you want to recover a directory from the previous commit, you can replace the commit hash by HEAD~1, for example:
git checkout HEAD~1 -- path/to/the/folder/
There are two easy ways to do this:
If the commit that included the errors only included the errors, use git revert
to invert the effects of it.
If not, the easy path is this:
git checkout 348…
cp -a path/to/the/folder ../tmp-restore-folder
git checkout HEAD # or whatever
rm -rf path/to/the/folder
mv ../tmp-restore-folder path/to/the/folder
git add path/to/the/folder
git commit -m "revert …"
For modern git (-s
or --source
):
git restore -s commit-sha-that-contains-dir relative/path/to/folder
man reference:
-s <tree>, --source=<tree>
Restore the working tree files with the content from the given tree. It is common to specify
the source tree by naming a commit, branch or tag associated with it.
If not specified, the contents are restored from HEAD if --staged is given, otherwise from the index.
As a special case, you may use "A...B" as a shortcut for the merge base of A and B if there
is exactly one merge base. You can leave out at most one of A and B, in which case it
defaults to HEAD.
git checkout -- path/to/folder/