Clone specific GitHub folder without full repository download

How can I download just a specific folder or directory from a remote Git repository hosted on GitHub?

For instance, consider the following GitHub repository:

git@github.com:foobar/Test.git

The directory structure is as follows:

Test/ ├── foo/ │ ├── a.py │ └── b.py └── bar/ ├── c.py └── d.py

I want to download only the foo folder without cloning the entire Test project. How can this be achieved?

Hey Kusha,

You can make use of Git Sparse Checkout. follow the below steps.

  1. Initialize a new git repository: mkdir Test cd Test git init

  2. Set the remote repository: git remote add origin git@github.com:foobar/Test.git

  3. Enable sparse checkout: git config core.sparseCheckout true

  4. Specify the folder to download: echo “foo/*” >> .git/info/sparse-checkout

  5. Download the specified folder: git pull origin main

Hello Kusha,

Well, I agree with @Mark Mazay , but you can also use the ‘svn’ command. GitHub repositories can be accessed using the svn command to download specific directories.

  1. Install svn (if not already installed): sudo apt-get install subversion

  2. Download the specific folder: svn export https://github.com/foobar/Test/trunk/foo

I hope this helps.

Hello kusha,

The better approach, according to me, would be using GitHub’s web interface.

You can just follow the steps given below.

  1. Navigate to the repository on GitHub.
  2. Browse to the foo folder.
  3. Click on the Code button (usually green).
  4. Select Download ZIP.
  5. Extract the ZIP file to get the foo folder.