34

I have a git repository that includes subdirectories. Example dirA/dirB. Is there any way to do a git clone on a Unix server to pull only files from a subdirectory (dirB)?

Is there some other git command other than clone that will do this?

How do I git a specific file?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
user1526912
  • 15,818
  • 14
  • 57
  • 92

6 Answers6

35

Suppose your project is in a dir called project, and you want only those commits which touch project/dirB.

Then:

git clone project/ subproject/
cd subproject
git filter-branch --prune-empty --subdirectory-filter dirB HEAD 

subproject will now contain the git history which touches dirB.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 2
    this worked great to grab only the initial sub-directory content, but when I executed a subsequent `pull` it merged with the original branch... – monkut Apr 02 '15 at 05:26
  • If you don't want the git history as a whole, add the `--depth 0` flag. –  Feb 04 '16 at 23:29
19

Cf https://www.kernel.org/pub/software/scm/git/docs/git-archive.html

With a shell command :

git archive --remote=<repo_url> <branch> <path> | tar xvf -
Bruno Thomas
  • 1,179
  • 17
  • 31
1

I would like to share a workaround. You can clone the entire repo, and then create a symulink for subdirectory:

mkdir <repo_dir>
cd <repo_dir>
git clone <repo_url>
ln -s <sub_dir> <your_location_where_you_want_to_checkout_sub_dir>

Again, this is not a solution - just a workaround. You will still clone the whole repo, and you can pull the changes only from . However, this was useful in some case of mine.

1

It's not really cloning, but if you just want a part of a project to start a new repo with, go into your repo and

git archive master <subdir> | tar -x -C /somewhere/else

Then go /somewhere/else and start a new repo there.

commonpike
  • 10,499
  • 4
  • 65
  • 58
0

Git works on the repository level. You will not be able to select only a subdirectory. If the repository that you are interested in has a web interface, you might be able to navigate to your subdirectory and grab it.

Nicolas Dudebout
  • 9,172
  • 2
  • 34
  • 43
  • Yes, git works on a repository, however, the desire to have a light checkout is high due to data mobility challenges. – cgseller Dec 27 '17 at 18:03
-1

If you want to clone only parts of your repository, you should look at git submodules.

The subdirectory is managed by a specific git repository and is referenced in the main repository as a submodule.

When you clone the main repository, the submodules are not automatically cloned.

Kowser
  • 8,123
  • 7
  • 40
  • 63
Benoit Courtine
  • 7,014
  • 31
  • 42
  • 5
    When you clone the main repository, the submodules are not automatically cloned? why? it absurd:( – hugemeow Aug 26 '12 at 05:59
  • 17
    This does not answer the question. It basically says: "I don't have an answer for your question, so I'll answer another question, because that's what I think you should do instead." – Eduardo Bezerra Feb 22 '16 at 13:53
  • @EduardoBezerra Exactly. I would prefer an answer and then a comment on alternatives. Check Bruno Thomas answer – Aalex Gabi May 27 '16 at 09:41
  • `Git sparse` might be a better option. Have a look at this (http://stackoverflow.com/a/13738951/431967) answer. – Highmastdon Apr 07 '17 at 09:58