World map

 

BM: Baca dalam Bahasa Melayu

World 10: Deep Git

Submodules vs subtrees

You will learn: two ways to keep another repo inside yours, and when to pick each.

The problem

Two shop apps use the same kuih-menu code, from its own repo. You fix a bug in it. How do both apps get the fix?

Pick one answer.

Watch

bash
git submodule add https://github.com/you/kuih-menu.git menu
cat .gitmodules
[submodule "menu"]
path = menu
url = https://github.com/you/kuih-menu.git
git submodule add puts the kuih-menu repo in a folder called menu. Then .gitmodules shows the path menu and the url.

A submodule: Another repo inside your repo, linked at one exact commit. is a repo inside your repo. Your repo keeps a link: the URL and one exact commit.

Try it

Build the command that clones the app and its submodules at once.

Tap the pieces in the right order.

…

Predict

Mei clones the app without --recurse-submodules. What is in the menu folder?

Pick one answer.

Watch

bash
git subtree add --prefix=menu https://github.com/you/kuih-menu.git main --squash
git subtree add copies the main branch of kuih-menu into the menu folder, squashed into one commit.

A subtree: Another repo's files copied into a folder of your repo, as normal files. copies the other repo’s files into a folder. They become normal files in your repo. Teammates need nothing extra.

Check

Submodule or subtree? Sort each fact.

Drag a card into a box. Or tap the card, then tap the box.

      You did it!

      You can now pick a submodule or a subtree, and explain why.

      Go deeper (optional)

      git submodule update --remote moves a submodule to the newest commit of its branch. Then commit that change. git subtree pull --prefix=menu <url> main --squash brings in new menu work. git subtree lives in Git’s contrib area, so check git subtree -h first. Sometimes a package manager is simpler than both.