Restore A Folder That's Deleted In The Local Branch
I'm trying to restore a folder I deleted in a local branch but I am having trouble doing it. I have created a new branch from main and deleted one of the folders, while I worked on the branch and did 10 commits later.
Now, I want to have the deleted folder in the working directory along with the git indexing.
I have tried -
git checkout HEAD~10 -- ./gt/foo/
referring to this question but I keep getting -
error: pathspec './gt/foo/' did not match any file(s) known to git
I have also tried -
git restore -s main -SW -- gt/foo
to pull the folder from main working branch but got the same error.
What can I do to restore this folder?
git log to get a list of all the commits that modified the folder. The most recent one will be the one where it was deleted. Here is an example repo, where I created and then deleted a folder called
$ git log -p -- hello/world
Then, we can take the commit before the one it was deleted by using
git restore --source=27f1f1adf~1 -- hello/world
This will restore the folder to the current directory without staging it, but you can stage it with git add:
Make sure that git log shows the name of the removed folder. If git log doesn't find it, you're searching in the wrong directory (are you at the root of the project? Did you get the path right?)
Once you've verified that the name and path are correct, you can actually combine both these steps in bash or zsh (replace with the name of your folder):
git restore --source="$(git log --pretty=format:"%H" -n 1 -- hello/world)"~1 -- hello/world
This will find the last commit that modified the folder (AKA, the one where it was deleted), and then it will restore the deleted folder:
- → Authenticate with a cookie using laravel 5.1 and jwt
- → Finding a specific GitLab tag from PHP
- → React: How to publish page on server using React-starter-kit
- → babel-loader, webpack, ES2015 modules: "Element type is invalid"
- → Create a function-attribute of a function, which is, in its turn, a method of an object literal
- → Model Validation in laravel 5.1 not working
- → GIT fatal: loose object
- → Laravel validation required rule not working
- → Axios array map callback
- → Where does this `webpack://` come from for `webpack-dev-middleware`?
- → error when trying to modify project in laravel forge
- → GitHub Pages and Jekyll content duplication and SEO issues
- → Use Laravel repositories with Datatables