Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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 -

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top