shallow git clone example |Video upload date:  · Duration: PT4M17S  · Language: EN

Quick guide to perform a shallow git clone and cut cloning time and bandwidth when working with large repositories.

Quick summary for impatient developers

Want a copy of a huge repo without waiting for a coffee break to finish? Shallow git clone buys you speed and bandwidth by pulling only recent commits. It is perfect when you just need to build, test, or inspect the latest code without hoarding the entire history like a digital pack rat.

Pick a depth and get only what matters

The key option is --depth. A depth of 1 gives you the working tree for the latest commit and almost nothing else. That means less disk usage and faster clones. Use a larger number if you need a bit of context but still want to save time.

git clone --depth 1 --branch main REPO_URL

If you do not want every branch to follow you home add --branch to limit the clone to one branch. That prevents unnecessary refs from being fetched and reduces surprise downloads.

Need older commits later

Shallow is not forever. If your debugging or bisecting skills demand history you can fetch more without recloning. Fetch a fixed number of commits or remove the shallow state and pretend you were full history all along.

  • git fetch --depth N to increase depth to N commits
  • git fetch --unshallow to convert the repo into a full clone

Use these when you discover some bug that lives in the past and you need context beyond the latest snapshot.

Submodules will bite if you are careless

Submodules often default to full history and will quietly ruin your bandwidth savings. Initialize them shallow to keep the plan intact.

git submodule update --init --depth 1

If you skip this you may see large unexpected downloads when your build starts pulling submodule history. That is what we call developer surprise.

Tags and annotated releases

Shallow clones can miss tag history and annotated tags. If your release steps rely on tags fetch them explicitly or unshallow first.

git fetch --tags --depth N
or
git fetch --unshallow

A short checklist for sensible shallow cloning

  • Decide how much history you actually need
  • Clone with --depth and --branch when applicable
  • Init submodules with --depth to avoid surprises
  • Fetch more history or unshallow when deeper context is required
  • Fetch tags manually if you depend on annotated tags

Final note with mild sarcasm

Shallow clones are a pragmatic trick for version control that save time and network cycles. They are not magic. If you need to investigate ancient crimes in the commit log you will have to fetch more history. Until then enjoy a faster clone and one less reason to complain about slow CI pipelines.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.