Skip to content

Instantly share code, notes, and snippets.

@Remi-Gau
Created July 25, 2020 05:59
Show Gist options
  • Save Remi-Gau/19985fa821ada40098ec6264231e4570 to your computer and use it in GitHub Desktop.
Save Remi-Gau/19985fa821ada40098ec6264231e4570 to your computer and use it in GitHub Desktop.
Dealing with submodule

repo_with_submod

More details here:

https://git-scm.com/book/en/v2/Git-Tools-Submodules

Adding submodules in the repo

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.

"Locking" a submodule to a specific version

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

Getting the latest changes from the remote repo for the submodules

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

To push all the changes including in the submodules

git push --recurse-submodules=on-demand

To clone the all repo and get all the right versions of the submodules

git clone --recurse-submodules https://github.com/Remi-Gau/repo_with_submod.git
cd repo_with_submod/
git submodule update --remote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment