Git - Submodules

Create Super-Repo

Add submodules to a git repository

mkdir modules
git submodule add gitolite@server:motd modules/motd

This will create a subdirectory motd in the modules directory and checkout the master branch of the motd repo. git creates a configfile .gitmodules where all references to submodules are recorded:

[submodule "modules/motd"]
path = modules/motd
url = gitolite@server:motd

Pull down new files to submodule

git submodule update

Clone Super-Repo

If you clone your super-repo containing submodules, you have to do some additional steps to populate your directories with files:

git clone gitolite@server:super-repo
cd super-repo
git submodule init
git submodule update
# to fetch new tags and move your entire super-repo to v0.0.2 you have to explicit do:
git fetch --tags
git checkout v0.0.2
git submodule update

Keep the Super-Repo uptodate

To checkout/clone new submodules in the existing clone of the super-repo, observe the following workflow:

git pull
git submodule init
git submodule update

Remove a Submodule

While adding new submodules via git submodule add … is easy, removing them is bit more complicated:

  1. remove the submodule config from .gitmodules
  2. stage your changes: git add .gitmodules
  3. also delete the submodule config from .git/config
  4. git rm –cached path_to/submodule_name
  5. cleanup your .git folder: rm -rf .git/modules/submodule_name
  6. commit
  7. remove untracked files: rm -r path_to/submodule_name

Change URL for a Submodule

To change the URL of a submodule (e.g. if it has been moved to another server) you can do that like so:

  1. change your .gitmodules file, replace the url part
  2. run git submodule sync
  3. optional: git submodule init; git submodule update
  4. add your changes to the index and commit
howto/git/submodules.txt · Last modified: 2015-08 by tb
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 ipv6 ready