0

I have following repository:

= dir 1
 -- subdir1
 -- subdir2

= dir 2

And I want to clone e.g. dir2 and dir1/subdir1. I followed instructions from this question: How do I clone a subdirectory only of a Git repository? and I've made this:

mkdir ${projectName} &&
cd ${projectName} &&
git init &&
git remote add -f origin <url> &&
git config core.sparseCheckout true &&
echo "dir2" >> .git/info/sparse-checkout &&
git config core.sparseCheckout false &&
echo "dir1/subdir1" >> .git/info/sparse-checkout &&
git pull origin main &&
git config core.sparseCheckout false

where appChoice is subdir1. After executing this command, I've get dir1 with both subdirectories: subdir1 and subdir2. Where is the mistake?

Raf1k
  • 31
  • 6
  • `git config core.sparseCheckout false` two times. The first one is certainly wrong, it affects `git pull` (`git checkout` after `pull`, to be precise). Not sure about the second one but anyway it's not needed. – phd Nov 29 '22 at 13:48
  • @phd without first ```git config core.sparseCheckout false``` I'm getting only dir2, whole dir1 is missing. – Raf1k Nov 29 '22 at 13:54
  • With `core.sparseCheckout` set to `false` you don't use sparse checkout at all so Git checks out everything. BTW, `projectChoice` and `appChoice` in your code are wrong, should be `dir2` and `subdir1`. – phd Nov 29 '22 at 13:58
  • 1
    Works for on a real clone. What I did differently: no `git config core.sparseCheckout false`; `echo "/dir1/subdir1/" >> .git/info/sparse-checkout` (leading slash for root, trailing slash for directories); `git fetch` then `git checkout master`. I really got `/dir2/` and `/dir1/subdir1/` full of files and nothing else. – phd Nov 29 '22 at 14:16
  • Thank you! maybe could you share me your full command? It will better for me to compare both.. – Raf1k Nov 29 '22 at 14:34
  • 1
    `mkdir test-sparse-checkout && cd test-sparse-checkout && git init && git remote add origin && git config core.sparseCheckout true && echo /common/web/ > .git/info/sparse-checkout && echo /dir2/ >> .git/info/sparse-checkout && git pull origin master && ls && ls common` The output from the 1st `ls` is "common dir2", from the 2nd just "web"; files are not listed but they are there, under `/common/web/` (but not in `/common/`) and under `/dir2/`. For the 1st `echo` I use `>` redirection to make sure the file is empty. – phd Nov 29 '22 at 14:49
  • @phd Yep, it's working. Thank you! But I've got another problem.. If I want to change e.g. /dir/subdir1/ to /dir1/${variable}/ it's not working properly, even though I'm sure that variable == subdir1 – Raf1k Nov 29 '22 at 15:58
  • 1
    `echo '${variable}'` (apostrophes) doesn't work, `echo "${variable}"` (double quotes) should work. – phd Nov 29 '22 at 16:16

0 Answers0