Skip to content

Instantly share code, notes, and snippets.

@demagu
Last active October 1, 2016 14:46
Show Gist options
  • Save demagu/5c6c4e6de745af99a5541f1c23d20517 to your computer and use it in GitHub Desktop.
Save demagu/5c6c4e6de745af99a5541f1c23d20517 to your computer and use it in GitHub Desktop.
Creating & Managing GIT submodules

Creating GIT submodules

On an existing repo:

Step 1:

git submodule add <remote repository> <local path>

This should return something similar to:

git submodule add git@github.com:demagu/ansible.git .ansible
Cloning into '/Users/user/Sites/raceroster.com/pursuit/.ansible'...
remote: Counting objects: 3773, done.
remote: Compressing objects: 100% (221/221), done.
remote: Total 3773 (delta 105), reused 0 (delta 0), pack-reused 3511
Receiving objects: 100% (3773/3773), 646.35 KiB | 0 bytes/s, done.
Resolving deltas: 100% (1526/1526), done.
Checking connectivity... done.

.gitmodules file should have been created with content similar to the one below:

[submodule ".ansible"]
	path = .ansible
	url = git@github.com:demagu/ansible.git

Step 2:

Initialize submodule:

git submodule init

Step 3:

Confirm status:

git submodule status

Should return something similar to:

1f5124a7c63c4d625c0e232251f4e503d0dea9ec .ansible (heads/master)

Switching branches for your GIT submodule

To switch your submodule to a different branch open your .gitmodules for editing (ie. nano .gitmodules) and add branch = your-branch below urlso your file looks similar to this:

[submodule ".ansible"]
	path = .ansible
	url = git@github.com:demagu/ansible.git
	branch = pursuit-local-env

Save and exit.

Then:

git submodule update --remote

which should result in:

Submodule path '.ansible': checked out '42f8ab3e68758ea2731942dbacfd7ee7dde3e281'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment