Skip to content

Instantly share code, notes, and snippets.

@sumardi
Created May 11, 2013 12:56
Show Gist options
  • Save sumardi/5559896 to your computer and use it in GitHub Desktop.
Save sumardi/5559896 to your computer and use it in GitHub Desktop.
Subdirectory checkouts with Git sparse-checkout
# New repository
mkdir <repo> && cd <repo>
git init
git remote add –f <name> <url>
git config core.sparsecheckout true
echo some/dir/ >> .git/info/sparse-checkout
echo another/sub/tree >> .git/info/sparse-checkout
git pull <remote> <branch>
# Existing repository
git config core.sparsecheckout true
echo some/dir/ >> .git/info/sparse-checkout
echo another/sub/tree >> .git/info/sparse-checkout
git read-tree -mu HEAD
# If you later decide to change which directories you would like checked out,
# simply edit the sparse-checkout file and run git read-tree again as above.
# http://schacon.github.io/git/git-read-tree.html#_sparse_checkout
@ankyhe
Copy link

ankyhe commented Sep 21, 2018

It seems you miss one step:
git config core.sparsecheckout true

@tkersten09
Copy link

tkersten09 commented Nov 13, 2018

Windows users be aware! The echo command in powershell and cmd creates the '.git/info/sparse-checkout' file with a weird "UCS-2 LE BOM" encoding (according to the notepad++ editor).
But git needs an encoding like "UTF-8", "UTF-8 BOM" or "ascii". Otherwise it won't download any directories and fails with a "error: Sparse checkout leaves no entry on working directory" error.

You can change that with notepad++ or use the out-file powershell command:

echo some/subdir/ | out-file -encoding utf8 .\.git\info\sparse-checkout

To add new lines to the existing file use the '-Append' option of the out-file command:

echo some/subdir2/ | out-file -encoding -append utf8 .\.git\info\sparse-checkout

source for echo in powershell encoding tip

@agalazis
Copy link

agalazis commented Jun 2, 2019

to specify what should not be checked out you can use the following pattern:

/*
!some_unwanted_directory/

reference: https://git-scm.com/docs/git-read-tree#_sparse_checkout

@zhaox27-medtronic
Copy link

the .git folder is still very large after performing the Existing repository script. How to fix?

@douglascayers
Copy link

@zhaox27-medtronic, a sparse checkout doesn't affect how much commit history is cloned into the .git folder, only how much is checked out into the current worktree.

To limit the size of .git folder you can change the depth that you clone/fetch (the depth is the number of commit history you download).

See git-fetch and look at the --depth flag.

@MosNotBuger
Copy link

if I use like this command line
echo some/dir/ >> .git/info/sparse-checkout
there will be a folder which names some ,
how could I set command that not produce this folder

@renjithps
Copy link

#This way will work. Please note the space between each path.
git init
git config core.sparsecheckout true
echo 'some/dir/' 'another/sub/tree/'| out-file -encoding ascii .git/info/sparse-checkout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment