8

I am new to Git. I want to clone a large remote repository lets say xyzcodebase. The structure of repo is like I have src folders for different functional areas as shown below.

xyzcodebase
|
|__ Func area 1
|
|__ Func area 2

I work on Functional area 2 hence while cloning I don't want code from Functional area 1 (large size) which will save lots of my time. So that after cloning I will have only Functional area 2 source code on my local repository.

Is there a way to accomplish such selective cloning of git repository?

Thanks in advance for your help

Vaman Kulkarni
  • 3,151
  • 2
  • 18
  • 19
  • see this thread: https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository – DoronG Nov 29 '17 at 13:56

3 Answers3

7

No, there is no way to only clone one directory. Git stores history as changes to whole repository instead of storing only subtrees as SVN.

If your project contains multiple functionalities, then maybe it would be better to split it into some sub-projects, with different repositories, and then include other repositories as submodules in master project. But it depends how your project is organized.

MBO
  • 27,873
  • 5
  • 47
  • 52
  • Thanks for your reply. If not clone, is it possible to fetch only one directory in an empty git repository? – Vaman Kulkarni Aug 18 '11 at 12:40
  • @Venam Still no, but you wouldn't need to. Once you have the whole repository, fetching one area or the other will be a relatively tiny operation. – meager Aug 18 '11 at 12:42
  • @Vaman Not really, unfortunatelly. `clone` is just fancy way to say `git init, git remote add, git fetch`, so you still have to wait for whole repository to transfer. You can fetch shallow copy (`--depth` param) of repository, but then without full history you can't commit and push to remote, so it's good only for deploy or code review, not for development (unless you'll send your changes as patches to someone who has full repository) – MBO Aug 18 '11 at 12:44
  • @meagar You're right, I forgot to mention that in Git one only has to fetch whole repository once, and then only fetch changes in compressed format – MBO Aug 18 '11 at 12:46
0

This is a hacky solution that might in special cases help with your issue. Specifically if you are only interested in a single file you could simply download it from the console (in Unix) via wget: wget https://raw.githubusercontent.com/GITHUBUSER/REPOSITORY/master/file.ending

You can get the link of a specific file by navigating to it in github and then click on "raw".

SCBuergel
  • 1,394
  • 14
  • 23
0

There is some support for this appearing recently, but the Git server needs to support partial clone so the objects from the other directory can be excluded from the generated pack file.

See also https://docs.gitlab.com/ee/topics/git/partial_clone.html#filter-by-file-path and git-sparse-checkout.

mdonoughe
  • 452
  • 5
  • 12