18

Our team is not using submodules anymore for a long time and we would like to free some repository space by deleting all cached submodules.

Do we simply need to delete the following?

rm -rf .git/modules

Or is there a more recommended way to do it? Also, we would like the deletion to be pushed to server if possible.

Note: Using git 2.5.4 and current state has everything merged in one branch.

Note: We do not remember the individual names of submodules that were used in old commits.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • `git submodule deinit` is mentioned here, http://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule – J.J. Hakala Jan 20 '16 at 02:12
  • Doesn't work: it still leaves a 170.7 MB `.git/modules` folder. – Cœur Jan 20 '16 at 02:20
  • https://gist.github.com/myusuf3/7f645819ded92bda6677 :: to effectively delete/replace a submodule. – parasrish Aug 20 '18 at 08:50
  • @parasrish you link to a gist in violation of license cc by-sa 3.0 with attribution required, because it is a non-credited copy of https://stackoverflow.com/a/1260982/1033581. On top of that, it doesn't answer the question because it assumes a known "_path_to_submodule_", which contradicts the question where I stated "_We do not remember the individual names of submodules that were used in old commits._" – Cœur Aug 20 '18 at 09:33
  • @Cœur could not get your remarks completely. However, w.r.t. "path_to_submodule", the query seems to be generic (as I had myself one), but not too specific as with "unknown submodule names". In such state, posting as an "explicit answer to the query avoided" and also "posting a new query was meaningless". Added useful information in comments hence. – parasrish Aug 21 '18 at 05:15

2 Answers2

45

I figured out a possible small confusion in my question regarding "not using submodules": are there unused submodule folders left, or were those folders deleted already and only cache is left?

Well, I think we can address both situations by first following a clean removal process, and then assume something was not done correctly and perform a manual cleanup.

The necessary steps for deleting one submodule are explained in How do I remove a submodule?. But to answer the question we will remove all submodules at once without assuming we know the names of the submodules.

# deinit all submodules from .gitmodules
git submodule deinit .

# remove all submodules (`git rm`) from .gitmodules
git submodule | cut -c43- | while read -r line; do (git rm "$line"); done

# delete all submodule sections from .git/config (`git config --local --remove-section`) by fetching those from .git/config
git config --local -l | grep submodule | sed -e 's/^\(submodule\.[^.]*\)\(.*\)/\1/g' | while read -r line; do (git config --local --remove-section "$line"); done

# manually remove leftovers
rm .gitmodules
rm -rf .git/modules

I do not know for server synchronisation. It could be done automatically with next commit, or we might need those commands:

git submodule sync
git submodule update --init --recursive --remote
Community
  • 1
  • 1
Cœur
  • 37,241
  • 25
  • 195
  • 267
7

With git 2.7

git submodule deinit mysubmod
git rm mysubmod
git commit -m "Remove mysubmod"
git push
rm -rf .git/modules/mysubmod

This updates .gitmodules and .git/config and removes mysubmod and .git there. Otherwise there will be problems if one wants to have some content in a directory named mysubmod.

In this case only the last part is left to be done. Since the submodules are basically just pointers to other repositories, there is not much to clean up unless those repositories that have been referred as submodules are to be removed. Working with the old commits of the repository may become more difficult.

J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
  • There is no folder for 'mysubmod' because we are not using submodules anymore for a long time. So all is left to do is `rm -rf .git/modules` as stated in the question itself? – Cœur Jan 20 '16 at 02:47
  • @Cœur Locally yes. On the server side there should be little submodule related data since the submodules are basically pointers to other repositories. – J.J. Hakala Jan 20 '16 at 02:54