17

Is there a way to clone only subset of the original branch in git?

E.g.

I have following folders structure:

A
|_ B
|_ C
|_ D

And after clone I want to have:

A
|_ C
|_ D

I want to use this flow for deploying my application:

  • To make clone faster by taking only part of the repo and ignoring unnecessary development files.
  • I don't want to have some files in production because of security reasons.

Any advice?

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
user1671010
  • 325
  • 4
  • 11
  • 2
    Short answer: no. Long answer: Use submodules – Nihathrael Nov 14 '12 at 09:40
  • I would also accept to have another branch for it (deployment branch), which would be in sync. – user1671010 Nov 14 '12 at 09:52
  • 1
    Possible duplicate of [How do I clone a subdirectory only of a Git repository?](http://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository) – Mike May 02 '17 at 00:09

3 Answers3

25

What you want is possible since Git 1.7 with what is called sparse-checkout.

A good explanation is found here.

Community
  • 1
  • 1
Avec
  • 1,626
  • 21
  • 31
2

Here's a modern approach to git sparse checkout

git clone --no-checkout <REPOSITORY> <DIR>
cd <DIR>
git sparse-checkout init --cone  # checkout root dir files (build, etc.)
git sparse-checkout set <SUBDIR1> [<SUBDIR2> ...] # subdir list to include
git checkout

See git documentation and GitHub blog post.

Sebastian Cichosz
  • 889
  • 13
  • 22
1

As the comment suggests, you can't clone part of a path. If you are used to different source control paradigms such as the one used by subversion, this may seem unintuitive at first. The reason you can't do it is because git stores snapshots of the whole tree. So you can get a shallow clone, meaning you get only recent history up to a point. This is more like a horizontal slice or cut-off, rather than the vertical one you are after.

The best way to get what you want is to use submodules. If you already have everything in one repository, you would have to either start fresh with this paradigm or tease apart the repository into multiple repositories with filter-branch. This would also be tricky as you would end up with empty commits in all repos and if get rid of those, you would have some tricky follow up filter branching to tie the proper commits in the submodule to the parent repository.

Take a look at the chapter on submodules at http://git-scm.com/book.

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141