Progress:
TIER 3 · MODULE 14· Expert

git read-tree

What 'switch' uses to load a different tree into the index.

🎯 What & why

git read-tree loads one or more tree objects into the index. It is the low-level engine porcelain like switch, checkout, and merge calls to swap what the staging area thinks the tree looks like.

🧠 Mental model

The index is a flat manifest. read-tree rewrites that manifest from a tree (1-tree mode), fast-forwards it (2-tree mode), or runs a 3-way merge of common-ancestor + ours + theirs (3-tree mode).

🛠️ Synopsis

git read-tree [--reset] [-u] [-i] [--prefix=<dir>/]
              [-m [--aggressive]] [--no-sparse-checkout]
              <tree-ish> [<tree-ish> [<tree-ish>]]

1 tree-ish: replace index. 2 tree-ish: fast-forward. 3 tree-ish: merge.

🎚️ Switches & options

FlagWhat it does
--reset⚠️ Replace index unconditionally; discard unmerged entries and local index changes.
-uAlso update the working tree to match (otherwise WT is left stale).
--prefix=<dir>/Read the tree into a subdirectory; index must be empty under that path.
-iDo not refresh stat-cache; useful when WT will not be touched.
-mPerform a merge (requires 2 or 3 tree-ish args).
--aggressiveResolve trivial 3-way cases (one side unchanged) automatically.
--no-sparse-checkoutIgnore sparse-checkout patterns for this read.

💡 Use cases

🧪 Examples

Reset index to HEAD (no WT touch)
git read-tree HEAD
Reset both index and working tree
git read-tree --reset -u HEAD
Read a tree into a subdirectory
git read-tree --prefix=vendor/foo/ <tree-sha>
3-way merge of three trees
git read-tree -m --aggressive $base $ours $theirs

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.