Checkout Specific Directory Within Git Repo
I believe that the first test of a truly great man is his humility. Really great men have a curious feeling that the greatness is not in them but through them. And they see something divine in every other man and are endlessly, incredibly merciful.
— John Ruskin.
One day I was working on a driver port to macOS (Apple Macintosh OS) and the only opensource code for it can be found on Linux kernel.
Heck! The Linux kernel repository is around 2GB including all history and I only needed a specific directory inside the repository. After searching the whole internet I found an answer1.
Here are the steps to clone a specific directory from a git repository:
- First and foremost you need to create a local blank repository on your workstation.
git init <repo-url>
- Inside the created bare repository, map the remote URL of the remote repository you want to clone.
cd <repo-name> git remote add origin <remote-repo-url>
- Then, setup the git config and specify that you’ll be doing a sparse checkout.
git config core.sparsecheckout true
- Create and add all the directories you want to checkout in the
sparse-checkout
file that can be found in.git/info/sparse-checkout
.echo "<needed-directory>/*" >> .git/info/sparse-checkout
- When all the above steps is done, finally pull the repository objects.
git pull --depth=1 origin master
So guys if you have any questions? hit me up on my social media accounts. That’s all there is that is needed. Now its already cloned and can now be worked on.
❌ Originally posted on August 5, 2019.