Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 724
mostwanted Avatar asked Mar 12 '12 16:03

mostwanted


People also ask

How do I restore a directory in git?

STRONG SUGGESTION: 1) Do a "pull" from your remote repo into a NEW repo (don't do any more damage to your local repo). 2) Try "checkout" ... or even "revert" in your new, local, repo: atlassian.com/git/tutorials/undoing-changes/git-revert. 3) Update the remote repo when you're sure everything is OK.

Is there a git restore all?

If you want to undo all of your current changes, you can use the git restore command with the "." parameter (instead of specifying a file path): $ git restore .

How do I revert to a previous state in git?

git reset --hard This command reverts the repo to the state of the HEAD revision, which is the last committed version. Git discards all the changes you made since that point. Use the checkout command with two dashes, then the path to the file for which you want to revert to its previous state.


2 Answers

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/  
like image 196
Carlos Campderrós Avatar answered Oct 11 '22 04:10

Carlos Campderrós


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 …"
like image 22
Daniel Pittman Avatar answered Oct 11 '22 02:10

Daniel Pittman