Page 1 of 1

Clone a Specific Subfolder from a GitHub Project

Posted: Tue Jul 12, 2022 2:18 am
by jussij
Checkout Process
Step 1 - Clone Repo
Clone the git repo using the <base url> as follows:

Code: Select all

git clone --depth 1 --filter=blob:none --sparse <base url>
Step 2 Checkout Subfolder
Go to the <repo folder> created by the clone and checkout the <subfolder> as follows:

Code: Select all

cd <repo folder>
git sparse-checkout set <subfolder>
Typical Example
As an example consider a repo with these base url:

Code: Select all

https://github.com/dotnet/samples
It contains the following subfolder:

Code: Select all

https://github.com/dotnet/samples/tree/main/core/interop/cpp-cli
That means these are the values needed for the commands:

Code: Select all

<base url>    => https://github.com/dotnet/samples
<repo folder> => samples
<subfolder>   => cpp-cli
NOTE: The <repo folder> is the name of the folder created by clone and should be the last element of the repo base URL.
NOTE: The <subfolder> is just the name of the folder which is represented by the last element of the subfolder URL.

Using those details the two steps for the checkout are as follows:

Step 1 - Clone Repo

Code: Select all

git clone --depth 1 --filter=blob:none --sparse https://github.com/dotnet/samples
Step 2 Checkout Subfolder

Code: Select all

cd samples
git sparse-checkout set cpp-cli
These steps will check out that subfolder:

Code: Select all

Volume in drive D has no label.
 Volume Serial Number is 7410-AB8C

 Directory of D:\Projects\samples\core\interop\cpp-cli

22/09/2022  09:41 AM    <DIR>          .
22/09/2022  09:41 AM    <DIR>          ..
22/09/2022  09:41 AM             3,853 CPP-CLI.sln
22/09/2022  09:41 AM               205 Directory.Build.props
22/09/2022  09:41 AM    <DIR>          ManagedApp
22/09/2022  09:41 AM    <DIR>          ManagedLibrary
22/09/2022  09:41 AM    <DIR>          MixedLibrary
22/09/2022  09:41 AM    <DIR>          NativeApp
22/09/2022  09:41 AM             2,684 README.md
22/09/2022  09:41 AM                34 snippets.5000.json
               4 File(s)          6,776 bytes
Cheers Jussi