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

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.

Hello, Can someone please help me with this issue

Unfortunately that’s not how the git repository storage structure works. In order to receive asamp, the parent trees referencing it must also be fetched. You can ask for certain objects to be filtered, but the storage is a tree structure and to obtain a reference all the parent trees need to be fetched and traversed.

Could you explain your use-case further?
Could you create a symlink to the inner directory if the omission of the parent structure is required merely for convenience?

You may also find the submodules feature useful in general, although it is not directly applicable to your existing repository.

1 Like

Hello @hchouraria - thank you for the reply; it is informative and good to know. So in short words it is not possible currently in gitlab (please correct me if I am wrong here because I want to convey this input to team), although it is nice feature to have because other vendors such as SVN, IBM RTC provides this feature.
Let me try to explain the use case - we are using hybris framework for web development, after installing this framework certain directory structure such as “hybris/bin/” is created as part of installation; under “hybris/bin” we create a folder named “custom”, under this folder we are trying to create git repository which will only checkout “asamp” folder from gitlab repository and other sibling folders going ahead in the future.
“Could you create a symlink to the inner directory if the omission of the parent structure is required merely for convenience?” - can you please elaborate this part in detail, I want to understand if creating symlink can actually be workaround to this issue.

This is a limitation of Git itself due to its different design. Directories in Git are just pointer-like objects, not first-class members like in Subversion, so they cannot be independently fetched and operated upon. Empty directories cannot exist in Git, as an example.

If you need to maintain each of your hybris/bin/custom/[dir] entries as independent project repositories with their own commits, while also maintaining a repository with the entire tree, the Git submodules feature can help you achieve that:

Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.

A few examples that show how to manage plugins/add-ons/extensions as their own repositories within a larger, parent repository, using submodules:

This will allow you to clone asamp separately when only working on that, while letting you keep asamp also as a part of the overall hybris framework project for builds, deploys, etc. of the whole.

In terms of managing such a setup, you’ll need to use tools to update the submodules to keep them current, in the parent repository. When cloning the parent repository, you may also need to pass --recurse-submodules to entirely clone all related sub-repositories together in one clone run (GitLab CI supports this).

Thanks for reply @hchouraria I will consider the option of Submodules