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-checkoutwith 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.