Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

github
communitycreator

How to download a single folder or directory from a GitHub repo

Anjana Shankar

Overview

Sometimes, we are only interested in a single folder from a repository. When we download a single folder, it allows us to work fast and not have to carry the baggage that sometimes comes with downloading the entire repository.

For example, a UI engineer might only want the UI folder from the entire micro-service repo.

Code example

We’ll use one of the public repos for this example. Here is the link to it.

mkdir Coding
cd Coding/
git init
git remote add origin -f https://github.com/anjanashankar9/Coding.git
vim .git/info/sparse-checkout
# Add leetcode to the file
git config core.sparseCheckout true
git pull origin master
ls
The commands to get started

Code explanation

  • Line 1: We create an empty directory on our machine.
  • Line 3: We go into that directory and initialize Git in this directory.
  • Line 4: We add the remote.
  • Lines 5 and 6: We have the sparse-checkout functionality. The files/folders that we like to checkout should be added to .git/info/sparse-checkout.

Note: In the vim, press i to open the file and write leetcode to edit it. To exit the vim, press escape and type :x to save and exit the file.

Once we set the config to sparse-checkout, we can do a pull.

Run the above commands in the terminal below:

Terminal 1
Terminal

We can think of the sparse-checkout file as the reverse of the .gitignore file.

This is an extremely handy trick for large repos, especially when we only have to work with a small number of files or folders from it.

RELATED TAGS

github
communitycreator
RELATED COURSES

View all Courses

Keep Exploring