Decorative image frame

Git Note

Git Object Model

git-obj-mdl

Git Submodule

Add Submodule

1
$ git submodule add <url> <path-to-submodule>

Confirm submodule is added by cat .gitmodules.

Set git config to show submodule summary when running git status:

1
$ git config --global status.submoduleSummary true
1
$ git config --global diff.submodule log

Add & Commit

1
2
$ git add .
$ git commit -m "add submodule"

Clone a repo containing submodules

1
$ git clone <url> <path>

If cd submodules/, nothing exists, need to do:

1
2
$ git submodule init
$ git submodule update

Then cd submodules/, everything is okay.

Switch submodule’s version

1
2
3
4
$ cd submodules/
$ git fetch
$ git checkout beta
$ cd ..

Run git status to see new commit in submodule.

1
2
$ git add .
$ git commit -m "change submodule to beta version"

Remove submodule

1
$ git submodule deinit <path-to-submodule>

At this point, we can reinit this submodule.

1
$ git rm <path-to-submodule>

Run git status to confirm.

1
2
$ git add .
$ git commit -m "remove submodule"

Multiple Nested Submodules

Clone a repo containing multiple nested submodules:

1
$ git clone --recursive <url> <path>

Run cat .gitmodules to confirm first layer of submodules.

To look into submodules’ cat .gitmodules, run:

1
$ git submodule foreach 'cat .gitmodules'

Update from submodules

1
$ git pull

Teammates may add/delete/change/redirect a submodule. Run:

1
$ git submodule sync --recursive

Then bring change for each submodule by:

1
$ git submodule update --init --recursive

Edit submodules

cd submodule/, git pull --rebase, make some changes, git commit -am "edit something, Not to push now.

cd .., run git status to see new commits in submodule, then commit in parent repository by git commit -am "edit submodule

If git push right now, would push a commit not yet pushed in submodule. Could check each indidual submodule carefully before push in parent repo, but might miss.

In git version > 2.7, can do:

1
git config --global push.recurseSubmodules on-demand

Then git push.