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/
Then, we can take the commit before the one it was deleted by using ~1
:
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: