0

I am in the process of porting my JS application to modern E6 modules. My application is stored in a git repo. Within the repo there's a submodule.

I created another branch to do what I need to do, but I would like to remove the submodule in this specific branch.

The reason is that I have to npm install the app within the submodule as a package rather than using it in "the classical submodule" way as I do in master branch.

Reading this answer I thought that would be as simple as checkout to the new branch, modify the .gitmodules file excluding the submodule and deleting the submodule folder itself.

However, when I do this, I checkout to master and it seems the submodule is missing from there as well, and this would be a tremendous error. I didn't stage the changes before checking out if this is important.

So, how do I get rid of the submodule in the new branch to start working in it, without loosing it in my master branch?

EDIT 1

I am trying again after phd comments. I am in the branch where I need to remove the gitmodule. I edited the .gitmodules file to remove the submodule i need to remove, and then tried to remove the submodule with git rm -r <my_submodule> but I get fatal: Please, stage your changes to .gitmodules or stash them to proceed message.

So I stashed my changes and removed the submodule. Then I was able to checkout to the master branch, and the submodule folder was there (as I needed).

Also, the master .gitmodules still showed the submodule I removed from the other branch, which was my aim.

So now I have a master branch with the submodule, and the "dev" branch without it and I can install with npm here. So I guess I messed up something and the asnwer I linked was right. Will accept phd answer as it was correct.

Finally, as I have git version 2.7.4 and I cannot run git checkout --recurse-submodules master, I added this GIT PPA (I am using Ubuntu) as the version in the OS package manager was older, upgraded git to 2.19, run the command and everything worked just fine.

The command I used are:

sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
apt-get install --only-upgrade git

EDIT 2

Strangely, I just noticed when I switch to the "dev" branch that the folder of the submodule I removed is still there, although the .gitmodules is not showing the submodule in the list. Also, during checkout to this branch I received the message warning: unable to rmdir 'ol/ol-layerswitcher': Directory not empty.

I guess that now this folder is not treated as a submodule anymore, but it is still there. How could I get rid of it from the dev branch and not from the master branch (which was my original question) if it is possible?

FINAL ANSWER

Ok answered myself. I just needed to remove the floder without git rm. Now the:

  1. the dev branch has nothing to commit
  2. the submodule folder is not there
  3. when I switch to it, I don't receive any message

FINAL ANSWER NOT TRUE!

Just wanted to add that deleting the files made them disappear also in the master branch. The only thing I could do apparently is living with the submodules folder in the devb ranch but just "ignoring" them with the .gitignore in order not to push them to my remote dev branch.

umbe1987
  • 2,894
  • 6
  • 35
  • 63

1 Answers1

2

Your question lacks a few important details so let's me guess the missing parts. You've created a branch, edited .gitmodules (removed the submodule from it) and removed the submodule's directory. You added and committed changes in .gitmodules.

I believe you committed .gitmodules because without that you couldn't checkout master branch.

Then you checked out master and found that the submodule directory is missing, right? And that the central problem of the question?

Unfortunately, that's how submodules work. Git doesn't update submodules automatically on checkout. You should be using git checkout --recurse-submodules master if your git is fresh enough.

Or you need to develop a post-checkout hook that updates submodules after checking out a branch. See an elaborated example.

phd
  • 82,685
  • 13
  • 120
  • 165
  • "I believe you committed .gitmodules because without that you couldn't checkout master branch." Actually, I did not... checking out only showed the modified/deleted files with M / D flags. My git is fresh (can't tell the exact version but it should be). "git checkout --recurse-submodules master" Will this add the submodule again only for master branch? Will try that. Thanks. – umbe1987 Sep 15 '18 at 15:14
  • 1
    With modified and not-committed `.gitmodules` git should refuse to checkout master. `git checkout --recurse-submodules` requires git 2.13+. – phd Sep 15 '18 at 15:16
  • I tried what you suggested and everyhing seemed to have worked (see EDIT 1 in my answer) and I accepted your answer. However, I switched back to the dev repo and I saw the submodule folder I removed again there (EDIT 2). What did I have done wrong? If you could help me again I'll be very thankful. – umbe1987 Sep 16 '18 at 08:52
  • Sorry, answered myself, see FINAL ANSWER. Thank you very much for your help! – umbe1987 Sep 16 '18 at 08:59