[QUICK-TIP] Git Submodules

I came across a directory within the DuckDuckGo Android app source code, that when clicked, linked directly to an external repository. I'd not seen this before, and thought it was pretty neat, and it could be insanely useful. I spent a short while Google'ing, trying to figure out the name of this concept, and how to execute it. Eventually found it- it is a Git Tool, called Submodules.

Quick Start

It's simple to add a submodule into your repository, with the following line:

git submodule add https://github.com/<user>/<repo> <path/to/save/at>

Additional Options

  • When cloning a project containing submodules, you will need to use the --recursive flag, if you wish to download all submodules also
  • To update the code within the submodules repositories, you will need to run git submodule update --init --recursive

Problems with Submodules

It's worth nothing, that this may work well for simple use cases, but would not be practical at all for referencing multiple packages. Usually a dependency management system (such as Cargo, NPM, RubyGems, Go Modules etc.) is a better solution.

Git doesn't automatically download submodules after clone (unless you use the --recursive flag), so if this is required for the project to run, you'll need to either document this, or add something into your build script to grab the submodules. Same goes for updates- submodules will not be fetched with a git pull, so git submodule update needs to be run.

There are also the potential security and stability issues this could cause, if you do not manage the repo being included. All in all, submodules are awesome, but for only a very particular use case.

Further Links

Example: https://github.com/duckduckgo/Android/tree/develop/submodules
Git Documentation: https://git-scm.com/book/en/v2/Git-Tools-Submodules
Git Module File Documentation: https://www.git-scm.com/docs/gitmodules
Git Submodule Documentation: http://www.git-scm.com/docs/git-submodule
GitModules File Documentation: https://www.git-scm.com/docs/gitmodules
GitHub Modules: https://github.blog/2016-02-01-working-with-submodules

You'll only receive email when Alicia's Notes πŸš€ publishes a new post

More fromΒ Alicia's Notes πŸš€