Today I learned how to create nested git repositories through the submodules
command.
I'm working on my Textual tutorial for EuroPython 2023 and I am saving all the tutorial materials (code, slides, instructions, etc) in a GitHub repository under my company's organisation.
The repository lives under my company because I am giving the tutorial in their name. However, I would still like to show that tutorial on my talks repository. So, I thought “Wouldn't it be nice if I could nest git repositories and just list the tutorial repository under my talks repository?”.
As it turns out, you can nest git repositories!
To nest a repository inside another one, you use the command git submodule add
.
When you add the nested repository, that git refers to as a “submodule”, you need to specify the URL of the original repository, so that git can point to it.
Suppose that you have two repositories, parent
and child
, and you want to nest child
inside parent
.
Here is how you could do this:
# Open the `parent` folder.
~:$ cd parent
# Clone the `child` inside the parent.
# This will create a folder `child` inside `parent`.
# (The child sub-repo can be inside a folder with a different name.)
~/parent:$ git clone https://github.com/xxx/child
# Add that folder as a submodule linked to a repo at a given URL.
# (This creates a new file `.gitmodules`.)
~/parent:$ git submodule add https://github.com/xxx/child child
# repository URL ^ local path for the repo ^^^^^
# Commit & push the submodule.
~/parent:$ git commit -m "Add child submodule."
~/parent:$ git push
That's all it takes! Quite neat, hun? If I learn other useful things about submodules I will let you know.
That's it for now! Stay tuned and I'll see you around!
+35 chapters. +400 pages. Hundreds of examples. Over 30,000 readers!
My book “Pydon'ts” teaches you how to write elegant, expressive, and Pythonic code, to help you become a better developer. >>> Download it here 🐍🚀.