prerequisites:
git >= 1.7.0
original article
(0. Create Empty Repository)
$ mkdir <repo>
$ cd <repo>
$ git init
$ git remote add -f origin <url>
Replace <url>
with the URL of your remote repository.
1. Do a Sparse Checkout
$ git config core.sparseCheckout true
Next, you need to include the folders and subfolders that you want to clone in .git/info/sparse-checkout
file.
Replace /path/to/dir
with the folder path.
$ echo "path/to/dir/" >> .git/info/sparse-checkout
2. Pull Main
$ git pull origin main
Notice:
main
is now the default branch of Github.
Since git 2.25.0
there is a new experimental feature of sparse-checkout. Here are some sample commands using sparse-checkout.
$ git sparse-checkout init # git config core.sparseCheckout true
$ git sparse-checkout set "/path/to/folder" # echo "/path/to/folder" >> .git/info/sparse-checkout
$ git sparse-checkout list # cat .git/info/sparse-checkout