How to fetch only one directory from a GitHub repo without cloning everything

I need to get just a specific directory from a GitHub repository without downloading the entire project. Let me explain my situation.

I have a remote repository that looks like this:

https://github.com/example/MyProject.git

The repo has this structure:

MyProject/
├── scripts/
│   ├── main.js
│   └── helper.js
└── docs/
    ├── readme.md
    └── guide.md

I only want to download the scripts directory. The whole repository is pretty large and I don’t need the docs folder at all. Is there a way to pull down just that one folder instead of cloning the complete repository? I’m looking for an efficient method that saves time and bandwidth.

Try git archive instead - it’s perfect for this. Run git archive --remote=https://github.com/example/MyProject.git HEAD:scripts | tar -x and it’ll grab just the scripts folder without any git history or metadata. You don’t need to set up a local repo or mess with sparse-checkout settings. The command pulls only that folder as a tar archive and extracts it right to your current directory. Way cleaner than sparse-checkout if you just want the files and don’t need to keep a git connection.

Skip the manual commands - just automate the whole thing.

I built a workflow that watches specific repo directories and pulls exactly what I need when stuff changes. No more remembering git commands or manual downloads.

It monitors your target directory and syncs only those files wherever you want them. You can even transform files during the sync if needed.

For your scripts folder, configure it once to track that path. Then it’s hands-off - checks for updates, pulls just your directory, and pings you when new files show up.

Perfect when you’re juggling multiple repos or need regular updates from specific folders. Beats running the same git commands endlessly.

Check out Latenode for this: https://latenode.com

you can also just grab it straight from GitHub’s web interface. hit up the scripts folder and either download the zip or copy the raw file urls. not as clean as using git, but it’s perfect when u don’t want to deal with sparse checkout nonsense.

The Problem:

You need to download only the scripts directory from a large GitHub repository without downloading the entire project. You’re looking for an efficient method to save time and bandwidth.

:thinking: Understanding the “Why” (The Root Cause):

Cloning the entire repository is inefficient when you only need a specific directory. A full clone downloads the entire Git history, which can be significantly larger than the project’s files themselves. This wastes bandwidth and increases download time, especially for large projects. Sparse checkout provides a solution by allowing you to selectively download only the parts of the repository you need.

:gear: Step-by-Step Guide:

Step 1: Initialize a Local Git Repository:

First, create an empty local Git repository where you’ll store the downloaded scripts directory. Open your terminal and navigate to the directory where you want to download the files. Then, run the following command:

git init

Step 2: Add the Remote Repository:

Next, add the remote GitHub repository to your local repository using the git remote add command. Replace <repository_url> with the actual URL of your GitHub repository:

git remote add origin <repository_url>  //e.g., git remote add origin https://github.com/example/MyProject.git

Step 3: Enable Sparse Checkout:

Enable the sparse checkout feature in your local repository. This feature allows you to specify which parts of the repository you want to download. Run this command:

git config core.sparseCheckout true

Step 4: Specify the Directory to Download:

Create a file named .git/info/sparse-checkout in your local repository. If this directory doesn’t exist, create it. Add the path to the directory you want to download (scripts in this case) to this file. The * is a wildcard which will download all files and subdirectories within the scripts directory. This ensures all files within scripts are downloaded.

scripts/*

Step 5: Pull the Selected Directory:

Finally, pull the specified directory from the remote repository. The following command fetches only the scripts directory and its contents:

git pull origin main

Step 6 (Optional): Verify the Download:

After running git pull, verify that only the scripts directory and its contents have been downloaded. Check the local directory using a file explorer or similar means to confirm your files are present.

:mag: Common Pitfalls & What to Check Next:

  • Incorrect Path: Double-check that the path in the .git/info/sparse-checkout file is accurate. A typo or incorrect path will prevent the correct files from downloading.
  • Branch Name: Make sure you are pulling from the correct branch (e.g., main, master). If your scripts folder is in a different branch, change origin main accordingly to point to the correct branch.
  • Sparse Checkout Configuration: Verify that git config core.sparseCheckout true was executed successfully. You might have to check for any permissions issues if this fails unexpectedly.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.