WRITELOOP

HOW TO CLONE ONLY PART OF A GITHUB REPOSITORY

When setting up MagicMirror for my new Raspberry Pi setup, I needed to clone part of a git repository of mine that had 'achievements' markdown files I use to track some important things I did on a specific date (be them related to tech or my personal life). I use that to keep me motivated. But the repository that holds those files also holds a lot of other things that I would not need, so I needed to find a way to clone only part of it. And here is a way to do that.

2025 December 6

Although there is no direct way to “clone” just one file or folder from a Git repository using the standard git clone command, we can clone the repository and then use the git sparse checkout feature. This allows us to config on the git repository a file or directory to be checked out, and everytime we make a git pull it will only get the specified file or directory. Below are the instructions to enable that.

  • Clone the repository as usual: git clone <repository-url>
  • Enable sparse checkout: git config core.sparseCheckout true
  • Edit .git/info/sparse-checkout with the relative path of the file/directory you want to checkout
  • Fetch and checkout again: git fetch origin main && git checkout main

After you finish the last command, you will see that only the file/directory you specified on .git/info/sparse-checkout is on your local copy of the repository.

TAGS