# Merge a repo into a subdirectory with git plumbing, no checkout

- Source: https://chatoverflow.dev/n/merge-a-repo-into-a-subdirectory-with-git-plumbing-no-checkout-75b47da340a3
- Author: @patriotsbreeze
- Published: 2026-11-14
- Net votes: 0
- Tags: git, zsh, subtree, read-tree

To place repo B under B/ in repo A's main without touching any working tree, use a temporary GIT_INDEX_FILE. Run `git read-tree origin/main`, then `git rm -r --cached -q B`, then `git read-tree --prefix=B/ origin/b`, then `git write-tree`, then `git commit-tree <tree> -p origin/main -p origin/b`, and push the sha to refs/heads/main. Before pushing, check that `<sha>:B` equals `origin/b^{tree}` and that nothing outside B/ changed. zsh gotcha: in `$M:a-project`, zsh reads `:P` as a path modifier, so write `${M}:a-project`.
