TechBuzzPro

How to Speed Up Git Clone and Fetch in Large Repos

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to reduce the time it takes to clone and fetch large Git repositories. You'll learn practical techniques—from shallow clones and sparse checkouts to tweaking Git's compression and concurrency settings—that can cut download times dramatically, especially on slow connections or with monorepos.

What You'll Need

  • A Git repository (local or remote) that you want to clone or fetch.
  • Git installed on your machine (version 2.20 or later recommended for most features).
  • Access to the remote repository (e.g., GitHub, GitLab, or a self-hosted server).
  • Basic familiarity with the command line.

Step-by-Step

  1. Use a shallow clone for the latest snapshot only. If you don't need the full history, clone with --depth 1 to fetch only the most recent commit. For example: git clone --depth 1 https://github.com/user/repo.git. This drastically reduces the amount of data transferred, especially if the history is large.
  2. For partial history, use a deeper shallow clone. If you need the last N commits, specify --depth N. For instance, git clone --depth 100 fetches the latest 100 commits. This is useful when you need recent changes but not the entire history.
  3. Convert an existing full clone to a shallow one. If you already have a full clone, you can fetch only recent history with: git fetch --depth 1. To remove old history locally, run git reflog expire --expire=all --all and then git gc --prune=all.
  4. Use sparse checkout to fetch only needed directories. For monorepos or large projects, clone with --filter=blob:none and --sparse. Then specify which subdirectories you want: git sparse-checkout set path/to/dir. This avoids downloading blobs for files you don't need.
  5. Enable partial clone with blob filtering. Run git clone --filter=blob:none --no-checkout https://github.com/user/repo.git. This fetches commits and trees but delays blob downloads until you check out files. You can also use --filter=tree:0 to skip trees as well, though that may slow down later operations.
  6. Increase Git's compression level for network transfer. Set git config --global core.compression 9 to compress data more, reducing bytes sent over the wire. This is especially helpful on slow connections, though it uses more CPU.
  7. Adjust HTTP post buffer for large pushes/fetches. If you're using HTTP(S), set git config --global http.postBuffer 524288000 (500 MB) to avoid errors on large requests.
  8. Enable parallel fetches for multiple remotes. If you fetch from several remotes, set git config --global fetch.parallel 8 to download from them concurrently.
  9. Use the --jobs option for clone. For a shallow clone, you can use git clone --depth 1 --jobs 8 to parallelize the fetch of multiple objects.
  10. Consider using a faster transport. If you're on a LAN, SSH is often faster than HTTPS. If you're on a slow WAN, consider using a local mirror or a CDN if available.

Common Mistakes

  • Using shallow clone when you need full history later. If you later need the entire history, you'll have to fetch it, which can be slower than a full clone initially. Use shallow clones only when you're sure you don't need the past.
  • Forgetting to unshallow when needed. If you need to deepen your shallow clone, use git fetch --unshallow to get the full history. Doing this later may be slower than a full clone from scratch.
  • Not combining sparse checkout with blob filtering. Sparse checkout alone still downloads all blobs. Use --filter=blob:none to avoid that.
  • Setting compression too high on a fast connection. High compression can slow down the process if your CPU is the bottleneck. Test different levels.
  • Ignoring that shallow clones break some Git features. Shallow clones can't be used with git bisect or git blame for commits outside the shallow boundary. Plan accordingly.

Tips

  • Use a mirror clone for CI or backup. If you need a full copy for backup or CI, use git clone --mirror to get all refs without a working tree, which is faster and smaller.
  • Clean up unnecessary remote tracking branches. After a fetch, prune stale branches with git fetch --prune to avoid storing extra refs.
  • Use Git LFS for large binary files. If your repo has large binaries, consider using Git LFS to store them outside the repository, reducing clone size.
  • Consider using a Git protocol that supports negotiation. Git's protocol v2 (default since Git 2.26) allows more efficient fetch negotiation, reducing the amount of data sent. Ensure your server supports it.
  • Monitor your network speed. If you're on a slow connection, even the best settings won't help much. Consider using a download manager or a proxy.

Troubleshooting

  • Clone fails with 'error: RPC failed; curl 56'. This often happens with large repos over HTTP. Increase http.postBuffer as mentioned above, or switch to SSH.
  • Shallow clone doesn't work with some servers. Some older Git servers don't support shallow clones. Update your Git client and server, or use a full clone.
  • Sparse checkout doesn't reduce download size. Ensure you're using --filter=blob:none along with sparse checkout. Without it, blobs are still downloaded.
  • Fetch is still slow after using filters. Check if the server supports partial clone. Some servers (e.g., GitHub) do, but others may not. If not, you may need to use a shallow clone instead.
  • High compression causes high CPU usage. If your machine is slow, reduce core.compression to 1-3. The default is usually 5.

Frequently Asked Questions

Will a shallow clone affect my ability to push?

No, you can push from a shallow clone, but you may need to deepen it if the remote rejects non-fast-forward pushes. In practice, pushing new commits works fine.

Can I use sparse checkout with an existing clone?

Yes. Run git sparse-checkout init --cone and then git sparse-checkout set dir1 dir2 to limit the working tree. However, this doesn't reduce the size of the .git directory unless you also use blob filtering.

How do I undo a shallow clone?

Run git fetch --unshallow to fetch the full history. This will download all missing commits, which can be large, but it's the standard way.

What is the difference between --depth and --filter?

--depth limits the number of commits fetched, while --filter controls which objects (blobs, trees) are downloaded. They can be combined for maximum effect.

Does Git LFS help with clone times?

Yes, if your repo has large binary files, Git LFS replaces them with pointers, so the clone only downloads the pointer files. The actual binaries are fetched on demand, which can be much faster.

Why is my fetch still slow even with --depth 1?

If the repo has many branches or tags, the server still sends refs. Use --single-branch to limit to one branch, and consider pruning tags with --no-tags.