0

I want to checkout only a folder from gitlab without its parent folder and other parent folder in the hierarchy.
Our gitlab repository structure is something like this hybris/bin/custom/asamp. There are other folders parallel to asamp folder. I only want to checkout asamp folder
I have been trying the solution given on this link - How do I clone a subdirectory only of a Git repository? but I am unable to download only asamp folder it also downloads complete hierarchy i.e. I can see hybris/bin/custom/asamp in my local repository. Please note that other folders which are siblings of asamp are not downloaded when I do the sparse checkout but complete hierarchy is checked out which I don't want. I only want asamp folder to be downloaded and not its parent folders.
I am using gitlab.

  • There's lots of ways to do this, but by far the easiest is for the upstream to maintain branches for the individual histories. Slicing and dicing histories involves tradeoffs and it's not clear what pain you're most interested in avoiding here; the reason Git is so efficient is it *doesn't* make everyone pay the overhead on every operation for flexibility that's rarely needed by anyone, with the downside that the people who do need it have some work to do. Please explain details of the situation you're in, including what specifically are the costs you don't want to pay, then I can help. – jthill Dec 02 '20 at 17:21
  • @jthill there is a hybris framework used by customer, after installing this framework customer is trying to get projects from custom directory such as ```asamp``` checked out directly into installation directory of hybris framework, this way they can test the changes quickly and finally push to gitlab – vikrant kamble Dec 03 '20 at 11:30

2 Answers2

0

In the post you linked : the answer uses some options (namely : --filter=blob:none) which require to be supported by the git server.

The author of this answer emphasizes that Github now supports it, and its support seems fairly recent -- the mention that "Github supports it" was added by the author on 2020-09-18 (two months ago), I haven't scanned github's changelog to see the exact date when they added the support.

You would have to check if Gitlab (or Gitlab community edition, if that's the one you use) specifically supports this feature. If it doesn't : there will be no easy way to download "only that folder" through git commands alone.


The regular Gitlab's web GUI, on the other hand, allows you to browse to any subdirectory in any commit, and download that subdirectory (download icon next to "Clone" button, then "Download this directory" choices).

Work out how the download url is built, and you can download the same archive with any web client (e.g : curl, wget ...)

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Hello LeGEC, this feature ```--filter=blob:none``` is supported in gitlab CE but I am seeing same problem i.e. parent folders in the hierarchy are checked out. – vikrant kamble Dec 02 '20 at 13:21
  • What command did you run to checkout only the subdirectory ? `git checkout master -- path/to/asamp` ? – LeGEC Dec 02 '20 at 13:27
  • yes, I ran the command as you mentioned ```git checkout master -- hybris/bin/custom/asamp``` – vikrant kamble Dec 02 '20 at 13:34
  • It is probably that part that isn't supported by gitlab. Try setting `GIT_TRACE=1 git checkout ...`, and perhaps `GIT_TRACE_PACKET=1 git checkout ...` to have more insight on what happens during that specific step. – LeGEC Dec 02 '20 at 13:39
  • Even the download also get complete hierarchy of folders i.e. when I try to download **asamp** directory then i get complete hierarchy download as ```hybris/bin/custom/asamp``` – vikrant kamble Dec 03 '20 at 11:25
0
git clone -n --depth 1 --filter=tree:0 u://r/l asamp; cd $_
git read-tree -um @:hybris/bin/custom/asamp
git reset --soft $(git commit-tree -p @ -m "just asamp" `git write-tree`)

will get you a minimum-download checkout of just that directory. Git (but not, I think, subset/superset alternatives like github) can even merge it back with a minimum-checkout merge and the -s subtree merge strategy.

To re-insert your work into the upstream history, you'll need to use that subtree merge strategy. git branch will tell you the name of the branch you started from, I'll assume it's master.

$ git branch
* asamp
  master
$

Now make a no-checkout scratch clone, and merge your work into the upstream master without checking it out:

git clone -nsb master -o local . `mktemp -d`; cd $_
git reset -q
git merge -s subtree local/asamp

Push it back to your local repo:

git push local master
cd -

and push that back to your upstream:

git push origin master
jthill
  • 55,082
  • 5
  • 77
  • 137
  • Hello @jthill thank you for the answer, I was able to checkout only **asamp** folder without parent folders in the hierarchy but in the push operation it completely messed up my repository, not to worry I am doing this practise on test gitlab instance. After running push command my entire repository got replaced with content of **asamp** folder, please check my comment below as the extension of this comment – vikrant kamble Dec 03 '20 at 08:49
  • Initially repository structure was ```hybris/bin/custom/asamp/*``` there were many folders under **custom** folder i.e. siblings of **asamp** folder but after running push operation content of the **asamp** folders were directly uploaded repository replacing everything it had before, i.e. at the root level there was **hybris** folder before but now at the root level there is content of **asamp** folder – vikrant kamble Dec 03 '20 at 08:52
  • Yes, you have to merge it back with the subtree strategy, which you have to do locally. To avoid having to check the entire thing out you need to do a minimum-checkout merge. I mistakenly inferred from "asamp" that you were looking at a sample or doing homework and would be pushing on to a second branch. You didn't mess up your repository, you can reset it back to the old tip. I'll edit in how to do minimum-checkout merges. – jthill Dec 03 '20 at 15:09
  • If your customer doesn't want to do the subtree mergeback, they can push the asamp branch they made, and you can merge it. – jthill Dec 03 '20 at 16:14
  • I am getting fatal: repository '/tmp/tmp.0qupSLMbof' does not exist error while executing the second clone command (git clone -nsb master -o local `mktemp -d`; cd $_), could you please check if that command is correct, shouldn't one specify the url to clone? – Grigor Aleksanyan Oct 18 '22 at 14:35
  • @GrigorAleksanyan right you are, I omitted the `.` path (to clone the current repo). Fixed, thanks. – jthill Oct 18 '22 at 14:59
  • Thanks for the quick response @jthill, now when I am trying to do the next step (git merge -s subtree asamp), I get the following error "merge: asamp - not something we can merge ". Could you please help me understand how the new cloned repo dir can know about asamp branch? – Grigor Aleksanyan Oct 18 '22 at 16:04
  • @GrigorAleksanyan fixed that, too, now :-.P Use `local/asamp`, git only creates the one branch ref in the clone and leaves the rest as just tracking refs. Sorry for the trouble, and thanks for sticking with it. – jthill Oct 18 '22 at 16:22