Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? ****
like image 740
artella Avatar asked Jan 02 '15 19:01

artella


People also ask

Does Git clean remove submodules?

The submod directory is a different git repository; if you want to remove it, Use -f option twice if you really want to remove such a directory . git clean -f -f -d submod does remove submod .

Where is my .gitmodules file?

The . gitmodules file, located in the top-level directory of a Git working tree, is a text file with a syntax matching the requirements of git-config[1].

Where is submodule hash stored?

It is stored in Git's object database directly. The tree object for the directory where the submodule lives will have an entry for the submodule's commit (this is the so-called "gitlink").

Do submodules automatically update?

Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated. When adding a submodule to a repository a new . gitmodules file will be created.


2 Answers

Initializing a git repo within dir2 (cd dir2; git init) doesn't make dir2 a submodule.

It just make dir2 a nested repo which will be ignored by any parent repo.
Deleting dir2 means you have no direct way to retrieve its content.

You could have done git submodule add /another/path/dir2, with dir2 a repo outside of dir1.
Then it would have been possible to restore dir2.

like image 64
VonC Avatar answered Oct 03 '22 20:10

VonC


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)

like image 37
Karmavil Avatar answered Oct 03 '22 19:10

Karmavil