Submodules allow you to keep a Git repository as a subdirectory of another Git repository.
If you don't want to modify the code, use a package manager instead (such as Maven or Rubygems) to declare your dependency.
A submodule is materialzied as:
Git sees it as a submodule and doesn’t track its contents when you’re not in that directory. Instead, Git sees it as a particular commit from that repository.
Used by arrow:
git submodule update --init --recursive # Needed for flight
git submodule add <url> <path>
# if your repository is empty
# cd submodule
# git init
# ...
This command create:
gitdir: ../.git/modules/path
[submodule "path_to_submodule"]
url = https://github.com/path_to_submodule
git -C <path> checkout <new version>
git add <path>
git commit -m "update submodule to new version"
where:
To list the submodules, use the status command
git submodule status
The repository location of the module is defined in the .git file
gitdir: ../.git/modules/path
After a commit of the first submodule, we see a:
2 files changed, 4 insertions(+)
create mode 100644 .gitmodules
create mode 160000 elastic
git clone --recurse-submodules https://github.com/username/project
Without the –recurse-submodules option, you get the directories that contain submodules, but none of the files within them. The submodule directories are there, but empty. You must run two commands to get the files:
The .gitmodules file in the project root (git working tree) stores the configuration such as:
[submodule "subModuleName"]
path = targetSubDirectoryName
url = https://github.com/username/repositoryname
[submodule "foo"]
active = false
url = https://example.org/foo
[submodule "bar"]
active = true
url = https://example.org/bar
[submodule "baz"]
url = https://example.org/baz
???
git config submodule.subModuleName.url YOUR_URL
git diff --cached --submodule
cd parentDirectory
git submodule update --remote # By default from the master branch
# Modifying the branch
# Track the change for the subModule not on master but on the branchv1
# -f .gitmodules add the config to the gitmodule file (for everybody then)
git config -f .gitmodules submodule.SubModuleName.branch branchv1
cd subdirectoryModule
git fetch
git merge
Verification:
git config --global diff.submodule log # setting the otuput default format of diff by setting the diff.submodule config value to “log”.
cd parentDirectory
git diff
By default, submodule are running in a detached HEAD state.
It means that there is no local working branch (like “master”, for example) tracking changes. With no working branch tracking changes, even if you commit changes to the submodule, those changes will be lost the next time you run git submodule update. To track its own change, we need to create a branch.
cd subModuleDir
git checkout myBranch
git submodule update --remote --merge
git submodule --help