How Git works · lesson 6 of 6
Commands and ideas in this lesson
Agit worktree adda second folder on another branch
Bgit submodule adda repository inside a repository
C--recurse-submodulesclone with the submodules filled in

Worktrees and submodules

Last updated

Two last tools that build on Git's model. A worktree lets you have two branches checked out at once, in two folders, sharing one repository. A submodule puts one repository inside another, pinned to a commit. The first is simple and handy; the second solves a real problem and brings several of its own. The sandbox doesn't have either, so this lesson shows them, and you can try them on your own computer.

By the end of this lesson you can

  • Use git worktree to work on two branches at once
  • Explain what a submodule is: a pinned link to another repository's commit
  • Clone a repository with submodules so they aren't empty
  • Say when a package manager or plain copy is a better choice

Two branches at once: worktrees

Ana is halfway through a new recipe on main when Lola Remy calls: the printed edition, on the release-1 branch, has a typo. She could stash (lesson 2.6), switch, fix, switch back and pop. Or she can give release-1 a folder of its own:

Terminal
git worktree add ../kusina-print release-1
Preparing worktree (checking out 'release-1')
HEAD is now at 3b5e0a1 Release the printed edition
git worktree list
/home/ana/kusina       9c41d27 [main]
/home/ana/kusina-print 3b5e0a1 [release-1]

~/kusina-print is a full working folder on release-1. Her half-finished recipe stays untouched in ~/kusina. Both folders share one repository: one set of objects and refs. A commit made in either folder is in the history straight away, with no push or pull between them.

  • One branch can be checked out in only one worktree at a time. Git refuses a second: fatal: 'release-1' is already used by worktree at '/home/ana/kusina-print'.
  • When she's done: git worktree remove ../kusina-print. The branch and its commits stay; only the folder goes.

Worktrees are handy for a quick fix, for reviewing a pull request without disturbing your work, or for running a long test on one branch while you keep working on another.

A repository inside a repository: submodules

Carla's sister keeps the family's sauce recipes in their own repository, sauces, which three other projects use. Kusina wants them too, without copying them in. A submodule puts that repository inside Kusina, in a folder, pinned to one commit:

Terminal
git submodule add https://example.com/reyes-family/sauces.git sauces
Cloning into '/home/ana/kusina/sauces'...
git status --short
A  .gitmodules
A  sauces
git commit -m "Add the family sauces as a submodule"

Two things went into the commit:

  • .gitmodules, a small text file that records the folder and the address.
  • A special tree entry for sauces, with mode 160000. Instead of a tree's hash, it holds a commit hash from the other repository. Kusina doesn't store the sauces' files; it stores "the sauces as of commit 7d2e4c1".

When the sauces repository gets new commits, Kusina doesn't see them until someone goes into sauces, checks out a newer commit, and commits the new pointer in Kusina.

The pitfalls of submodules

The pin is the point: Kusina always builds with a known version of the sauces. But it makes everyday work fiddly:

  • Cloning gives an empty folder unless you ask for the submodules (step 5).
  • Pulling doesn't update them. After git pull, run git submodule update too, or set git config submodule.recurse true.
  • Inside the submodule you're in detached HEAD (lesson 3.5), on the pinned commit. Commits made there, without switching to a branch first, are easy to lose.
  • Two pushes. Change the sauces from inside Kusina, and you must push the sauces repository first, then Kusina's new pointer. Push only Kusina, and everyone else's clone points to a commit they can't fetch.
  • Merge conflicts on the pointer show two commit hashes, and choosing means understanding both repositories.

Do you need a submodule?

Ways to use code or files from another repository
WayGood forCost
A package manager (pip, npm, cargo)Code that’s published as a package, with version numbersNeeds the other project to publish packages
A submoduleAnother repository you need at an exact commit, and may changeThe pitfalls in step 3, for everyone who clones
A subtree (git subtree)Copying another repository’s history into a folder, and pulling updates now and thenA bigger history; sending changes back is awkward
A plain copyA few files that rarely changeUpdates are by hand

For most projects, a package manager is the easiest. Use a submodule when you really need another repository, at a pinned commit, inside yours.

Spot the trap: the empty folder

Spot the trapJoaquin

Joaquin clones Kusina on a new laptop. The sauces folder is there, but empty. “Someone forgot to commit the sauces.”

What happens if…

Terminal
git submodule update --init --recursive
Submodule 'sauces' (https://example.com/reyes-family/sauces.git) registered for path 'sauces'
Cloning into '/home/joaquin/kusina/sauces'...
Submodule path 'sauces': checked out '7d2e4c1f0a9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d'

Check what you learned

Ana makes a commit in the worktree ~/kusina-print. When can she see it from ~/kusina?
What does Kusina store for the sauces submodule?
You clone a project and a submodule folder is empty. What do you run?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git