How to create a git worktree of sub path

137 Views Asked by At

Is there something similar to git worktree add directory commit-ish:SUB_PATH?


Use Case

I have the following structure

- alpha
  |- VERSION
    \- file.txt
  \- sub
- beta

And the goal is to check out the repo as following

- .git
- alpha
  |- v1
    |- .git
    \- file.txt
  |- v2
    |- .git
    \- file.txt
  |- v3
    |- .git
    \- file.txt
  \- sub
- beta

Where each of v1, v2 and v3 represent the name of a branch. And it's desired to minimize the scripting, and rely mostly on git commands.


Achieved Output

What I was able to reach so far is

by first sparse-checkout all directories except for alpha\VERSION and then worktree add the branch to alpha\v1. As seen below

- .git
- alpha
  |- v1
    |- .git
    |- alpha
      \- VERSION
        \- file.txt
      \- sub
    \- beta
  |- v2
    ...
  |- v3
    ...
  \- sub
- beta

Using the commands

git init
git config core.sparseheckout true
echo alpha/SUB > .git/info/sparse-checkout
echo beta >> .git/info/sparse-checkout
git remote add origin -f PATH_TO_REPO
git pull origin master
git worktree add alpha/v1 v1

Is it possible to worktree add only a subdirectory to reach the desired output?

0

There are 0 best solutions below