More details here:
https://git-scm.com/book/en/v2/Git-Tools-Submodules
I add 2 dummy repo I created on my account as submodules
mkdir subfun
cd subfun
git submodule add https://github.com/Remi-Gau/submod_1.git
git submodule add https://github.com/Remi-Gau/submod_2.git
This will create a .gitmodules
file with the following content.
[submodule "subfun/submod_1"]
path = subfun/submod_1
url = https://github.com/Remi-Gau/submod_1.git
[submodule "subfun/submod_2"]
path = subfun/submod_2
url = https://github.com/Remi-Gau/submod_2.git
FYI : that file NEEDS to be committed like any other file for those changes to be version controlled.
The repo submod_1
has 2 tagged versions v0.0.1
and V0.0.2
. I want to make sure I only use the v0.0.1
here so I go in the folder that contains the the repo submod_1
and I create and checkout a branch (that's done in one command with git checkout -b
) that I call version1
by specifying the tag I want to move to (v0.0.1
)
cd submod_1
git checkout -b version1 v0.0.1
A way to make this "permanent" on a project is to tell get to track the specific branch of the submodule all the time.
So we can create a remote branch by doing
git push --set-upstream origin version1
And then going out of the submodule and telling git to track only that branch
cd ..
git config -f .gitmodules submodule.submod_1.branch version1
The content of the .gitmodules
file should now read.
[submodule "subfun/submod_1"]
path = subfun/submod_1
url = https://github.com/Remi-Gau/submod_1.git
branch = version1
[submodule "subfun/submod_2"]
path = subfun/submod_2
url = https://github.com/Remi-Gau/submod_2.git
This will get the latest version of the submodules (for the branches tracked by that submodule).
git submodule update --remote
To get the latest version and update your local submodule do
git submodule update --remote --merge
git push --recurse-submodules=on-demand
git clone --recurse-submodules https://github.com/Remi-Gau/repo_with_submod.git
cd repo_with_submod/
git submodule update --remote