Clone single folder : GitHub
This guide describes how to clone a single folder (directory) on a specific branch from a GitHub repository (repo).
Prerequisites
Before you proceed complete the following:
Topics in this guide
Clone a single folder and branch from GitHub
-
Gather the following details for the GitHub repo (or fork) that contains the files you want to clone.
- URL of the GitHub fork/ repo; for example,
https://github.com/mkavana/repo-example
- Name of the branch that the required files are on; for example,
Mod02-branch
- Path to the (sub) directory that contains the required files; for example,
Mod02-example-directory/sub-directory/
- URL of the GitHub fork/ repo; for example,
-
Open a web browser, then go to github.com, and sign in.
-
Go to the URL of the GitHub fork/ repo you want to clone.
For example, go to
https://github.com/mkavana/repo-example
. -
Select the Code dropdown, then copy the clone URL to your clipboard.
For example, copy the URL *
https://github.com/mkavana/repo-example.git
to your clipboard. -
On your computer, open the Visual Studio Code editor (VSC).
-
In VSC, from the top menu, select Terminal > New Terminal or use the shortcut keys CTRL + SHIFT + ‘.
-
In the VSC TERMINAL window, change into the directory where you want to clone the fork/ repo.
For example, to change into the directory myGitDirectory on the path C:/, enter the following command into the VSC terminal.
cd C:/myGitDirectory
-
Create an “empty” repo on your computer by cloning a single branch from GitHub, and by pasting the clone URL you copied previously.
Note: In the following example, the single branch is named Mod02-branch and the clone URL for the fork/ repo is
https://github.com/mkavana/repo-example.git
. Replace the example branch name and the clone URL with the branch and fork/ repo you want to clone.Enter the following command into the VSC terminal.
git clone --filter=blob:none --sparse https://github.com/mkavana/repo-example.git --branch Mod02-branch --single-branch
-
After cloning is complete, change into the directory containing the repo you cloned.
Enter the following command into the VSC terminal.
cd repo-example
-
Initialize the repo with the
sparse-checkout
command.Enter the following command into the VSC terminal.
git sparse-checkout init --cone
-
Checkout the specific sub-directory you require.
For example, to checkout a sub-directory named Mod02-example-directory/sub-directory/, enter the following command into the VSC terminal.
git sparse-checkout add Mod02-example-directory/sub-directory/
-
Close the VSC terminal panel by using the X icon (top right side of the panel).
-
In VSC, from the top left menu bar, select File > Open Folder.
-
In the VSC Open folder pane, navigate to the where you cloned the fork/ repo on your computer, select the repo directory, and then choose Select Folder.
For example, navigate to the directory c:\myGitDirectory, and select the repo directory repo-example.
-
Confirm that VSC is switched to the correct branch.
Note: In VSC, the current branch is indicated in the status bar in the bottom left.
In the following image, the current branch is named Mod02-branch.
-
Use the VSC EXPLORER pane to locate and open the sub-directories and files you need to edit.
For example, locate the the file name 01-nested-file .md on the path repo-example-repo/Mod02-example-directory/sub-directory/.
-
Edit the file(s), then save, stage, commit, and push your file changes to GitHub.
Appendices
Check the following supplementary Appendices for more details and context.