How to restore a whole directory from history of git repository?

Git

Git Problem Overview


I would like to restore a whole directory (recursively) from the history of my git repository.

There is only 1 branch (master).

I know the commit where errors were included.

Can I use the sha1 hash of the parent commit to restore the state of the directory as it was before the errors were included?

I thought about something like this:

git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 path/to/the/folder/

but it did not work.

Git Solutions


Solution 1 - Git

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/ 

Solution 2 - Git

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:

  1. git checkout 348…
  2. cp -a path/to/the/folder ../tmp-restore-folder
  3. git checkout HEAD # or whatever
  4. rm -rf path/to/the/folder
  5. mv ../tmp-restore-folder path/to/the/folder
  6. git add path/to/the/folder
  7. git commit -m "revert …"

Solution 3 - Git

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.

Solution 4 - Git

If you simply do git checkout <SHA-ID> then it will temporarily move you to that sha-commit.

Each commit object holds the entire structure of the disk at that time, so if you have files there and need to copy them out, you can do so. Warning though, you will not be in any branch, so you'll have to move back to master before copying the file into your working tree and commit it.

Solution 5 - Git

git checkout -- path/to/folder/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmostwantedView Question on Stackoverflow
Solution 1 - GitCarlos CampderrósView Answer on Stackoverflow
Solution 2 - GitDaniel PittmanView Answer on Stackoverflow
Solution 3 - GituptoyouView Answer on Stackoverflow
Solution 4 - GitMark FisherView Answer on Stackoverflow
Solution 5 - Gitamalik2205View Answer on Stackoverflow