The aim is to clone https://github.com/opower/sensu-metrics-relay/tree/master/lib/sensu/extensions instead of the whole directory.

git clone https://github.com/opower/sensu-metrics-relay/tree/master/lib/sensu/extensions

results in:

Cloning into 'extensions'...
fatal: repository 'https://github.com/opower/sensu-metrics-relay.git/lib/' not found

No, it isn't.

To reduce the download size, you can truncate history instead, using --depth=1.

| improve this answer | |

With modern git you have the "subtree" feature that allows exactly for this.

It will create a new branch with all commits recreated (hence new IDs) to filter out only one directory out of the repository.

Then you can add the repository as a remote, and use only that branch.

You can find a long explanation at https://medium.com/@porteneuve/mastering-git-subtrees-943d29a798ec but note that since this was written things changed in git, now subtree is a core feature.

The complete git documentation for the feature is at https://git.kernel.org/pub/scm/git/git.git/plain/contrib/subtree/git-subtree.txt

The third example is almost exactly what you need:

EXAMPLE 3. Extract a subtree using branch
Suppose you have a source directory with many files and
subdirectories, and you want to extract the lib directory to its own
git project. Here's a short way to do it:

First, make the new repository wherever you want:

    $ <go to the new location>
    $ git init --bare

Back in your original directory:

    $ git subtree split --prefix=lib --annotate="(split)" -b split

Then push the new branch onto the new empty repository:

    $ git push <new-repo> split:master

But note that whatever you do, your question is quite odd in git "ideology", so it smells of an XY problem. You should share more details on why do you think you need to do this exactly, because you may just be on a wrong path anyway.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.