Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the workflow to clean git submodules on clones?

There are useful answers how to remove submodules "locally" - How do I remove a submodule?

However I have the issue I have clones of my repo on several machines. Two I only work once or twice a month. So when I update the branches on those although the submodules are no longer tracked the files are still on the working branch as well in .git/modules. On more than one occasion I accidentally checked some in. In other cases I have builds failing due to the presence of these unwanted files/directories.

I suppose I could keep a list of stuff to delete - but that doesn't seem right - how about another person would do the removal and I don't have info outside git what to remove?

So what is the suggested way to clean up clones?

update

For me git --version returns git version 2.18.0 which seems to be current (it is 2018-09-01).

I add a reproducible example.

Setup

mkdir parent
cd parent 
git init
git submodule add https://github.com/jakesgordon/javascript-tetris.git
git commit -am add

cd ..
git clone parent clone
cd clone
git submodule update --init

Now both directories contain the submodule with checked out files in javascript-tetris/. .gitmodules contains the submodule.

When I do

cd parent
git rm javascript-tetris
git commit -am delete

In parent the directory is javascript-tetris gone and the entry in .gitmodules removed. But there remains a populated .git/modules/javascript-tetris directory.

on the clone side:

git pull

gives the warning: warning: unable to rmdir 'javascript-tetris': Directory not empty. And the directory remains, also .git/modules/javascript-tetris is still there.

like image 490
bdecaf Avatar asked Aug 22 '18 09:08

bdecaf


People also ask

How do I clone a git repository with submodules?

The list of steps required to clone a Git repository with submodules is: Issue a git clone command on the parent repository. Issue a git submodule init command. Issue a git submodule update command.

How do submodules work in git?

A git submodule is a record within a host git repository that points to a specific commit in another external repository. Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated.

What is git clean command?

To recap, git clean is a convenience method for deleting untracked files in a repo's working directory. Untracked files are those that are in the repo's directory but have not yet been added to the repo's index with git add .


1 Answers

Is this helpful to you?

git clean -xfd
git submodule foreach --recursive git clean -xfd
git reset --hard
git submodule foreach --recursive git reset --hard
git submodule update --init --recursive

UPDATE

To remove a submodule you need to:

  • Delete the relevant section from the .gitmodules file.
  • Stage the .gitmodules changes git add .gitmodules
  • Delete the relevant section from .git/config.
  • Run git rm --cached path_to_submodule (no trailing slash).
  • Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  • Commit git commit -m "Removed submodule "
  • Delete the now untracked submodule files rm -rf path_to_submodule

    Source

like image 69
StaticVariable Avatar answered Sep 19 '22 13:09

StaticVariable