0

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?

weshouman
  • 632
  • 9
  • 17
  • Why do you want to put worktrees inside of other worktrees? – Guildenstern Apr 25 '23 at 13:34
  • What would “worktree add only a subdirectory” do, concretely? I fail to understand what worktrees have to do with paths. – Guildenstern Apr 25 '23 at 13:34
  • 1
    that means to get a worktree of a subdirectory of the repo at a specific commit/branch instead of getting a worktree of the repo's root at a specific commit/branch – weshouman Apr 25 '23 at 13:43
  • based on my basic knowledge worktree does not do that. I'm not sure whether my knowledge is incomplete? or if there's some command that could do that ...? – weshouman Apr 25 '23 at 13:45
  • `man git worktree` says that “--no-checkout can be used to suppress checkout in order to make customizations, such as configuring sparse-checkout.”. Maybe try `--no-checkout` and see if that helps? – Guildenstern Apr 25 '23 at 13:50
  • Combining that with another sparse checkout for the worktrees, allows getting `alpha\v1\alpha\VERSION\file.txt` without having the other directories/files but still we can't reach `alpha\v1\file.txt*` – weshouman Apr 26 '23 at 09:36

0 Answers0