Git Submodule

发布于 2018-04-29 09:49:34

git submodule

# git add submodule
git submodule add <url> [<target-dir>]
# submodule needs to be updated manually
git submodule update --init --recursive

# git clone repo with sumodules
git clone --recursive <project url>

# switch a normal directory to submodules
cd <sub-dir>
pwd # (double check before proceeding!)
git filter-branch --subdirectory-filter <dir-name> -- --all

settings about submodule

git config --global status.submoduleSummary true

will produce:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
  new file: .gitmodules
  new file: vendor/plugins/demo
Submodule changes to be committed:
* vendor/plugins/demo 0000000...fe64799 (3):
  > Fix repo name for main project companion demo repo

Other config:

git config --global fetch.recurseSubmodules on-demand
git config --global diff.submodule log

update submodule from remote

Method 1:

git submodule update --remote [<submodule-dir>]

Method 2:

git submodule sync --recursive [<submodule-dir>]

Method 2:

cd <path/to/module>
git fetch
git checkout -q <commit-sha1>
cd -
git commit -am “Updated submodule X to: blah blah”

Danger Zone! Pitfalls Interacting with Remotes

  • always publish the submodule change before publishing the change to the superproject that references it.
  • always remember to commit all your changes before running git submodule update as if there are changes they will be overwritten
comments powered by Disqus