How to remove Git submodules in your Drupal repository

Most of you using Git to version control your Drupal website for quite some time may have come across submodules when committing a contrib module. 

So what is submodules in Git. In basic terms, submodules allow you to keep a Git repository as a subdirectory of another Git repository. In other words, submodules are Git repositories nested inside a parent Git repository at a specific path in the parent repository's working directory. A submodule can be located anywhere in a parent Git repository's working directory and is configured via a .gitmodules file located at the root of the parent repository.

When it is a good time to use submodule? it is useful when you want to share code that you also need change along with the 3rd party vendor or developer of that code. If you're not trying to change the shared code along with the 3rd party of that code, then it's better to avoid this complicated procedure, unless you are an expert.

There are advantages and disadvantages, Randy Fay has a clear and well written summary on his blog: Drupal deployment with Git submodules

In my situation, i really didn't need to go down this route for my Drupal development. So if you find yourself in similar situation, i hope you will find this useful. A word of caution, i'm not entirely sure if this is a good or bad standard practice of working with Git but it works for me and reduces the complication of having to deal with submodules. 

When i initialise, added, commit and push my changes to master repository, i had noticed on Github some of the contrib modules that were supposed to be files and directories were in fact a reference of some kind. You can tell by the icon displayed next to it and you can't click through to further examine the directory's content since it is no longer a directory but a sub repository in the master repository. This is known as a submodule in Git.

Below image shows these submodules in my Git repository.

submodules in Git

 For my Drupal development, i have no need to commit them as submodules in my repository but unfortunately, it came as part of the contrib module upon fetching it via Composer. Submodule contains its own .git file in the directory. This may make a lot of sense to the module developer in his workflow but i clearly did not need this and adding more complications.

So how do we remove the submodule and treat is like the rest of your other contrib module in Git?

This is the way i handled it.I'm still learning Git so if you have a better solution or a proper way of handling this procedure, please let me know in the comments below. 

Let's begin by finding out what is going on with your Git repo. I ran git status to see the results of any current changes and I get this message:

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

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
	modified:   web/modules/contrib/mailchimp (modified content, untracked content)
	modified:   web/modules/contrib/media_entity_soundcloud (modified content, untracked content)
	modified:   web/modules/contrib/mobile_device_detection (modified content, untracked content)

no changes added to commit (use "git add" and/or "git commit -a")

This tells me my local repository is up to date with my master branch (the remote repository) but i get a warning about modified files not commit. Odd because i had added and commit all files and directories so why are these left unchanged while other contrib modules are have been committed? 

After some research, i discovered these directories contained a .git file which meant they are sub repository from an external source which now lives inside of my master branch (repository). So i have sub repositories inside my main repository. There are benefits to doing this but in my case, it was just complicating my development and i do not intend to ever make any code changes to these modules as i only needed the include them in my Drupal site for the functionality i needed. It must be noted, a lot of contrib modules do not contain .git files.

Soon I had learnt how to remove them as submodules and commit it to the repository like all other contrib modules. Please be aware, I am not sure if this is a good solution or standard practice but it seems to work and i have no issue with my Git repository after this process. 

First, I went to Github to examine my master remote repository. There i discovered some contrib modules where in fact different from the rest. It had an icon showing a directory and an arrow inside and you could not click to view the content inside. It was some kind of reference, then soon learnt these were in fact pick up as submodules by Git and treated as such. 

Now i know which contrib modules were submodules. Note: comparing what i saw on my Github repository, there were actually a few more which git status did not show in the message. I don't know why so github ignore the git status and referred to github to see which module i have to remove it as submodule. 

The way i remove a contrib module as submodule is as follow:

- Login to your account and view your master repository. Find the contrib module that is a submodule. for example:

submodules in Git

Notice the directory with an arrow inside. That is a submodule. For the purpose of this article, let's pick this one (realname module) to work on.

- In you command line, go to root of your local repo (in this case, it's the root of your Drupal installation). 

- Git may have created a file called .gitmodules in the root of your local repository to track your submodule. We will delete this file:

git rm .gitmodules

- Stage the changes:

git add .gitmodules

- Delete the .git of the submodule from realname module

rm -rf web/modules/contrib/realname/.git

- Run, for example: git rm --cached path_to_submodule (no trailing slash):

git rm --cached web/modules/contrib/realname

- Run, for example: rm -rf .git/modules/path_to_submodule

rm -rf .git/modules/web/modules/contrib/realname

- Submodule of realname module should now be untracked so we can add it as files and directory as part of this local repository. Run:

git add web/modules/contrib/realname

- Commit the changes with:

git commit -m "Removed submodule of realname module and no longer tracked"

- It can now be added to your remote repository, run:

 git push -u origin master

- Repeat the steps above for each of the submodules you are to remove.

Lastly, you can go view your remote repository on github to see if the icon next to the your module you worked on (in the case of this article, it was realname module). The icon should look like a folder without the arrow inside and you'll be able to view the content inside of this folder. This means, we have remove the submodule. 

[A lot of the steps were lifted of this page:]


The content of this field is kept private and will not be shown publicly.
Your email address will be kept private and will not be shown publicly.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.