A good[ish] website
Web development blog, loads of UI and JavaScript topics
What’s a Git submodule? How to add, use and remove them? Plus some third party scripts to help deal with them in general.
2015-01-17 Added: method to update a project submodule.
Submodules are great! But at times, a bit hard to deal with. This post tries to make that easier.
Git submodules enable a kind of nesting of Git projects, Git documentation sums it nicely:
It often happens that while working on one project, you need to use another project from within it. Perhaps it’s a library that a third party developed or that you're developing separately and using in multiple parent projects. A common issue arises in these scenarios: you want to be able to treat the two projects as separate yet still be able to use one from within the other.
WordPress in this case:
$ git submodule add git@github.com:WordPress/WordPress.git wp
That pulls in a WP, basically it does the same as git clone
, pulls a project in. But it’s also added as a submodule now.
If a repository that has submodules is cloned, say, from GitHub, then the submodules need to be initiated to pull them in, the clone
command doesn’t do anything for the submodules:
$ git submodule update --init
The git submodule update --init
step can be skipped if using the --recursive
flag when coning:
$ git clone --recursive git://github.com/user/some-project.git
Lot harder than adding one. I’ll use the WP that I added earlier as an example:
.gitmodules
file..gitmodules
changes git add .gitmodules
.git/config
.git rm --cached path_to_submodule
(no trailing slash).rm -rf .git/modules/path_to_submodule
git commit -m "Removed submodule <name>"
rm -rf path_to_submodule
git submodule deinit
was introduced in v.1.8.0.
From git docs:
Unregister the given submodules, i.e. remove the whole
submodule.$name
section from.git/config
together with their work tree.
It does two things:
.git/config
.When using deinit
, the submodule removal workflow looks like this:
git submodule deinit <path_to_the_module>
.gitmodules
file..gitmodules
changes git add .gitmodules
git rm --cached path_to_submodule
(no trailing slash).rm -rf .git/modules/path_to_submodule
git commit -m "Removed submodule <name>"
rm -rf path_to_submodule
It’s not that different from the non-porcelain method to be honest.
$ cd submodule
$ git pull origin master
$ cd ..
$ git commit ./submodule -m "Updated submodule reference"
$ cd submodule
$ edit some-file.js
$ git add some-file.js
$ git commit -m "Tweaked some-file.js"
$ cd ..
$ git add submodule/
$ git commit -m "Updated submodule reference"
$ cd submodule
$ git push origin master
Lets upgrade WP to version 4.1
:
$ cd wp
$ git fetch && git fetch --tags
$ git checkout 4.1
$ cd ..
# Now git status shows a dirty tree
$ git add wp
$ git commit -m "Upgrade to WP 4.1"
Git-extras has a git delete-submodule
command. See here how to install git-extras. And here's the authors screen cast on it also.
[vimeo slug_hash=45506445]
Hope this helps, bye.
Comments would go here, but the commenting system isn’t ready yet, sorry.