How to checkout or clone a folder without downloading its parent folder

944 views Asked by At

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.

2

There are 2 answers

5
LeGEC On

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 ...)

8
jthill On
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