Since doing this can be a repetitive task in many projects, I have created two helper functions based on awesome @Fatih answers with few modifications and store them in my machine using a custom bash shell file.
These functions can be invoked in any folder without the need of remembering or typing all of the implementation details.
Here are the steps to make life easier:
In your user root path create a file something like
.myBashCommands.sh
Write the following contents in it, If you already have your own dedicated file for custom functions, feel free to use it instead.
#!/bin/bash
# Find and log all git sub-modules if exist without making any changes.
# Credits: https://mcmap.net/q/1413790/-automatically-add-all-submodules-to-a-repo
function logAllGitSubModulesIfExist() {
for x in $(find . -type d) ; do
if [ -d "${x}/.git" ] ; then
cd "${x}" ;
origin="$(git config --get remote.origin.url)" ;
cd - 1>/dev/null;
echo "found gitsubmodule at =>" "${origin}" "${x}" "\n";
fi ;
done
}
# This loop first finds directories only, looks for a .git directory, identifies the original URL and then adds the submodule.
# Credits: https://mcmap.net/q/1413790/-automatically-add-all-submodules-to-a-repo
function addAllGitSubModules() {
for x in $(find . -type d) ; do
if [ -d "${x}/.git" ] ; then
cd "${x}"
origin="$(git config --get remote.origin.url)"
cd - 1>/dev/null
git submodule add "${origin}" "${x}"
fi
done
}
Close the file and while you are still in the same working directory, Refresh your terminal so that it recognize your newly added methods.
Here is how to do it: -
source .myBashCommands.sh
That's it! Now you are ready to reuse the two functions in any project and in any folder.
To invoke the methods simply type the name.
For example to log all available submodules just type the following in your terminal
logAllGitSubModulesIfExist
Similarly to find and add all existing submodules, just type the following: -
addAllGitSubModules
~/.vim/bundle/matchit
for example which is cloned straight from github – Erato