Restoring deleted submodules
Asked Answered
H

4

12

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? ****
Helpful answered 2/1, 2015 at 19:15 Comment(11)
git submodule update --init doesn't do the trick?Lack
@Lack : No I get a fatal: repository 'dir1/dir2' does not exist error messageHelpful
And what if you mkdir dir1/dir2 before the git submodule update --init? Also, what version of Git are you using?Lack
@Lack : No it still complains fatal: repository. I guess this must be because the .git files are missing from within dir1/dir2Helpful
What version of git are you using? .git is not in a submodule root folder for quite some time now.Lack
@Lack git version 2.1.3. But dir1/dir2 will have git files because I make it a repository (via git init from within dir2) before I add it as a submodule to dir1 (see commands above)Helpful
Ok, a nested git repo then, not a submodule. Did you push dir2 to an upstream repo?Lack
@Lack : No I did not push dir2 to upstream repo.Helpful
Then it is the equivalent of deleting a local repo... nothing much to do beside trying a few of those unerase programs to see if the filesystem has somehow kept a trace of the deleted files.Lack
@Lack : Ah ok. I think I must be misusing the submodule features. I was just playing with the functionality to see how it works. ThanksHelpful
If you only have a .gitmodules file and have deleted the submodules repositories, try this script: gist.github.com/aroemen/5027030Discontinuance
L
4

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.

Lack answered 3/1, 2015 at 10:16 Comment(1)
Just to add, for the setup you have mentioned above your original command git submodule update --init restores dir2Helpful
G
7

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)

Gervais answered 18/2, 2022 at 23:19 Comment(2)
When running git commands there has to be a git repository in the current working directory. For example, if my folder ~/home/myrepo has a git repository, I need move my current working directory there, to that directory, to run git status. Git will return that exact message if you run git status (or any other git command) from a folder that does not contain a git repositoryGervais
Maybe you downloaded and extracted a zip or rar file in your "submodules" or "dependencies" folder. That could be the reason there is no git repository in your submoduleGervais
L
4

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.

Lack answered 3/1, 2015 at 10:16 Comment(1)
Just to add, for the setup you have mentioned above your original command git submodule update --init restores dir2Helpful
P
3

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

git submodule deinit -f .
git submodule update --init
Packer answered 2/12, 2021 at 10:4 Comment(1)
this works when tried after deleting submodule files to restore them..Chard
P
-1

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

git reset --hard
Pair answered 6/5, 2021 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.