'Restoring deleted submodules

Suppose that I have a submodule dir1/dir2 (created via the steps shown below). How do I restore the submodule dir2 after having deleted it?

git submodule update complains that the submodule does not exist, and git reset HEAD --hard restores dir2 but not its contents. I am creating the submodule in the following way :

mkdir dir1
cd dir1/
mkdir dir2

cd dir2/
touch 1.txt
git init
git add 1.txt
git commit -m "test"

cd ..
git init
git submodule add ./dir2/
git commit -m "adding submodule"

rm -r dir2
**** Now how do I restore dir2 and its contents? ****


Solution 1:[1]

In case you didn't commit the changes (at least) you can try this. It worked for me

git restore path-to-your/submodule-name --recurse-submodules

In my case, I think the restore didn't work because it had submodules, and this solved it.

But most important I could restore the undesired changes made to the submodule (a bunch of binaries creating warnings)

Solution 2:[2]

Try to "deinit" and "init" all the submodules by the following two commands:

git submodule deinit -f .
git submodule update --init

Solution 3:[3]

If you did not commit your deletion you can just commit all your other local changes and then do

git reset --hard

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karmavil
Solution 2 Unique
Solution 3 rufreakde