Eric Sun
Eric Sun

Reputation: 913

git : how to perform some operation to some specific submodule

Git has the foreach command to recursively enter each submodule and so some operations. I now want to narrow the scope to some specific ones.

For example, there are 100 submodules inside a git repo. At some point, several of the submodules get dirty, the dirty submodules can show up with git status -sb. I only want to execute git clean for these dirty submodules.

I can use git submodule foreach git clean, however this recursively enter 100 submodules and perform the git clean, but most of them are not needed and waste a lot of time.

What I want is some kind of git submodule foreachdirty, or at least git submodule <submoduleName> to execute command on specific ones. Is there such facility existing?

Upvotes: 3

Views: 846

Answers (2)

Eyal Gerber
Eyal Gerber

Reputation: 1667

Another possible solution is to do the following using git submodule:

git submodule foreach 'if [ -n "$(git status --porcelain)" ]; then git clean -f; fi'

or if you know the names of the submodules upfront you can do the following:

git submodule foreach 'if [ "$name" == "submodule1" ] || [ "$name" == "submodule2" ]; then git clean -f; fi'

Note: if the submodule is located in a directory within your parent repository then the name of the submodule will include the relative path to it. For example if the submodule path is: ParentRepo/Dir1/Dir2/SubmoduleFolder then then the name of the subrepo would be Dir1/Dir2/SubmoduleFolder.

Upvotes: 1

LeGEC
LeGEC

Reputation: 52151

Note that "running a git action in a submodule" is equivalent to :

cd path/to/submodule
git action

or

git -C path/to/submodule action

If you have a way to list your dirty submodules, you can simply run a loop. For example in bash :

# if you have listed them in a file, the command can simply be 'cat dirtylist.txt'
<command to list submodules> | while read submod; do git -C $submod clean -fd; done

Upvotes: 2

Related Questions