gitgit-submodules

How to clean and hard reset specific git submodule?


I have multiple git submodules in my project and often time need to both git clean and
git reset --hard all of them. (e.g to have clean CMake build tree)

To achieve that I run from root project directory the following:

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

However sometimes I want to do this only on a specific submodule and not all of them.

How can I do this from repository root directory without cd into specific submodule and then doing the same with:

cd path/to/submodule
git clean -xfd
git reset --hard
cd ../../.. # Back to project root directory

Reason why I want to do it from project root is to be able to define VSCode task that handles only specific submodule with these git commands by using input variable to specify which submodule to handle.

EDIT:

I managed to do it for git clean with:

git clean -xfd path/to/submodule

However the same thing can't be done for git reset --hard apparently because no option to specify path:

https://git-scm.com/docs/git-reset

If there is a method how to remove build directory in submodule with cmake command that should work too.


Solution

  • You could leverage the name variable and use an if condition to check whether the submodule name matches the specific submodule, e.g.:

    $ git submodule foreach --recursive 'if [ "$name" = "specific_submodule" ]; then git clean -xfd && git reset --hard; fi'