Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting all Git cached submodules from repository

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.

like image 380
Cœur Avatar asked Jan 20 '16 02:01

Cœur


People also ask

What does git rm R -- Cached do?

--cached. Removes the file only from the Git repository, but not from the filesystem. By default, the git rm command deletes files both from the Git repository as well as the filesystem. Using the --cached flag, the actual file on disk will not be deleted.

How do I remove a .git from a folder?

Just run the rm command with the -f and -r switch to recursively remove the . git folder and all of the files and folders it contains. This Git repo remove command also allows you to delete the Git repo while allowing all of the other files and folder to remain untouched.


2 Answers

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
like image 180
Cœur Avatar answered Sep 18 '22 09:09

Cœur


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.

like image 42
J.J. Hakala Avatar answered Sep 18 '22 09:09

J.J. Hakala