Git Clone Specific Directory
While you cannot clone only a single directory from repository there is still one workaround to this problem.
i.e. Using sparse-checkout
feature.
You can specify name of the directory in .git/info/sparse-checkout
file only checkout it. It makes it visible in your working directory. This saves disk space and time if the repository is very large and you only need a small part of it.
1. Initialize an empty Git repository
git init cd repo-name
2. Configure sparse-checkout
git config core.sparseCheckout true
3. Add the remote repository
git remote add origin repo-url
4. Specify the directories in you want to clone
# For example for 'Frontend/Java-Login-App', you would add: echo "DevOps-Project-01/Java-Login-App/" >> .git/info/sparse-checkout
5. Pull the files
git pull origin main
However it still has some pros and cons:
Full Repository History: Even with sparse-checkout, the entire repository's Git history (commits, branches, tags) is still downloaded. Only the working directory files are filtered.
Large Repositories: For extremely large repositories, you might combine this with a shallow clone git clone --depth 1 repo-url
to reduce the history downloaded, and then apply sparse-checkout.
Updating: When you git pull later, Git will continue to only check out the directories you've specified in .git/info/sparse-checkout
. If you need to add more directories, you'll need to edit that file and then run git read-tree -m -u HEAD
to update your working directory.