Home > database >  how to undelete a folder that was a submodule removed via git rm
how to undelete a folder that was a submodule removed via git rm

Time:10-15

I need to undelete a non-commited git rm of a folder that was showing as a submodule.

This is more complex than you might expect.

In my private repo on GitHub I found a submodule. This was unexpected. The GitHub project page showed the submodule as a folder with an arrow on it, but was not clickable.

locally there was a folder of the submodule's name and in it a .git folder. Indicating that it had been git init locally.

Locally I renamed the .git folder in the submodule folder and ran from the parent git rm -f folder

thinking it would get rid of the submodule and just leave me with the real folder which I can then git add back into the repo

but it deleted the folder locally and now none of the following commands seem to be able to get it back. Remember, the folder that was listed as a submodule has never been committed to a git server. But the folder that it was in has.

Tried all of the following

git reset HEAD folder
git add folder
git checkout -- folder
git reset HEAD folder/*

and updated git to 2.33.1 to also try

git restore folder
git restore folder/*

and

git status

currently shows

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    folder

.git/config has no reference to the submodule

Nothing I read in this thread helped Restore a deleted folder in a Git repo

CodePudding user response:

In my private repo on GitHub I found a submodule. This was unexpected. The GitHub project page showed the submodule as a folder with an arrow on it, but was not clickable.

This indicates that the submodule is ... incomplete at best, and at worst, quite broken.

Locally there was a folder of the submodule's name and in it a .git folder. Indicating that it had been git init locally.

Possibly—or perhaps you or someone ran git clone to create it. Either way, that .git directory (folder) contained the actual repository.

If there are other clones of that particular repository, those other clones exist, and have whatever state of up-to-date-ness that they have. If not, they do not exist.

Locally I renamed the .git folder in the submodule folder and ran from the parent git rm -f folder

That's all fine, except for one crucial piece of information. You renamed (or moved) this .git. Where did you put it?

Let's set up a similar situation. Note the long warning and hint sequence that Git prints here:

$ cd ~/tmp
$ mkdir tt
$ cd tt
$ git init
Initialized empty Git repository in .../tt/.git
$ mkdir sub
$ cd sub
$ git init
Initialized empty Git repository in .../tt/sub/.git
$ echo for testing > README
$ git add README
$ git commit -m initial
[master (root-commit) 1fd3599] initial
 1 file changed, 1 insertion( )
 create mode 100644 README
$ cd ..
$ git add sub
warning: adding embedded git repository: sub
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint: 
hint:   git submodule add <url> sub
hint: 
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint: 
hint:   git rm --cached sub
hint: 
hint: See "git help submodule" for more information.

What this has done is prepare my next commit with a gitlink.

A gitlink is the most important half of a submodule. The other half, which is also the most important,

  • Related