Skip to content

Instantly share code, notes, and snippets.

@T1T4N
Last active July 11, 2022 10:06
Show Gist options
  • Save T1T4N/c104c564f07d59903ca06aeae5b044be to your computer and use it in GitHub Desktop.
Save T1T4N/c104c564f07d59903ca06aeae5b044be to your computer and use it in GitHub Desktop.
Git sparse checkout

Introduction

Starting with Git 2.37.0 there are a lot of improvements, particularly the full integration and rollout of sparse index. Using partial checkouts can really help the speed of git commands, among others committing, amending, stashing and querying status. This guide is to be considered experimental, so please don't modify/destroy your main repo checkout. A personal recommendation: use a sparse worktree

Usage

Note: the sparse-checkout.txt file mentioned below is a simple newline separated list of relative directories within the repository.

Regular repo

  1. git clone --no-checkout "ssh://git@github.com/path/to/repo.git" sparse-repo
  2. cd sparse-repo
  3. git sparse-checkout init --cone --sparse-index
  4. git sparse-checkout set --stdin <<< $(cat sparse-checkout.txt)
  5. git checkout main

Worktree

  1. cd main-repo-dir
  2. git worktree add ../sparse-repo --no-checkout
  3. cd sparse-repo
  4. git sparse-checkout init --cone --sparse-index
  5. git sparse-checkout set --stdin <<< $(cat sparse-checkout.txt)
  6. git checkout <branch>

Issues

Converting a worktree or repository to sparse breaks powerlevel10k/gitstatus integration.

References

  1. Highlights from git 2.37
  2. git sparse-checkout
  3. Make your monorepo feel small with git sparse-index
  4. Bring your monorepo down to size with sparse-checkout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment