This guide describes how to clone a single folder (directory) on a specific branch from a GitHub repository (repo).

Topics in this guide

Clone a single folder and branch from GitHub

  1. 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/
  2. Open a web browser, then go to github.com, and sign in.

  3. Go to the URL of the GitHub fork/ repo you want to clone.

    For example, go to https://github.com/mkavana/repo-example.

  4. 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.

    alt text

  5. On your computer, open the Visual Studio Code editor (VSC).

  6. In VSC, from the top menu, select Terminal > New Terminal or use the shortcut keys CTRL + SHIFT + .

    alt text

  7. 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
    

    alt text

  8. 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
    

    alt text

  9. After cloning is complete, change into the directory containing the repo you cloned.

    Enter the following command into the VSC terminal.

     cd repo-example
    
  10. Initialize the repo with the sparse-checkout command.

    Enter the following command into the VSC terminal.

     git sparse-checkout init --cone
    

    alt text

  11. 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/
    

    alt text

  12. Close the VSC terminal panel by using the X icon (top right side of the panel).

    alt text

  13. In VSC, from the top left menu bar, select File > Open Folder.

    alt text

  14. 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.

    alt text

  15. 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.

    alt text

  16. 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/.

    alt text

  17. 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.