Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@gadmyth

This comment has been minimized.

Copy link

gadmyth commented Aug 11, 2013

It still makes a full checkout ;(

@jholcomb-agrisolutions

This comment has been minimized.

Copy link

jholcomb-agrisolutions commented Jan 29, 2016

Works perfectly, and only pulls the directory I want.

@ManfredBartz

This comment has been minimized.

Copy link

ManfredBartz commented Aug 10, 2017

@gadmyth I have the same problem.
It still makes a full checkout, ;(

@mk-pmb

This comment has been minimized.

Copy link

mk-pmb commented Aug 20, 2017

@ManfredBartz , @gadmyth: Which version of git were you using?

@udaykalluri

This comment has been minimized.

Copy link

udaykalluri commented Dec 1, 2017

echo some/dir/ >> .git/info/sparse-checkout
echo another/sub/tree >> .git/info/sparse-checkout
explain above lines i can't understand

@askalski85

This comment has been minimized.

Copy link

askalski85 commented Jan 2, 2018

>> means append to a file on the right text from echo command on the left
.git/info/sparse-checkout is a configuration file for your sparse checkout (you can use some GUI text editor if you prefer and then skip the lines with echo)

@cecekpawon

This comment has been minimized.

Copy link

cecekpawon commented Jan 14, 2018

Beware of that bad dash in –f: should be -.

@cppljevans

This comment has been minimized.

Copy link

cppljevans commented Jan 16, 2018

1st I tried the "# New repository" script, but got "fatal: refusing to merge unrelated histories". Then, realizing I was using an existing repository, tried the "# Existing repository" script, but that got "error: Sparse checkout leaves no entry on working directory" and nothing
changed in the working directory. Please, what's wrong?

@koifans

This comment has been minimized.

Copy link

koifans commented Jun 8, 2018

A trick that people sometimes may ignore is that the "path" that you write into .git/info/sparse-checkout file is not exactly copied from the URL in browser. For example, I want to pull folder moodle-scalable-cluster-ubuntu from the URL https://github.com/Azure/azure-quickstart-templates/tree/master/moodle-scalable-cluster-ubuntu, then just put "moodle-scalable-cluster-ubuntu" or "moodle-scalable-cluster-ubuntu/" or "moodle-scalable-cluster-ubuntu/*" into the sparse-checkout file. Do NOT add some prefix like "/tree/master/".

Another thing that might help is to remove the CRLF in the end of each line of the sparse-checkout file. I didn't have any problem with that myself but someone said it's a trick for them.

@ankyhe

This comment has been minimized.

Copy link

ankyhe commented Sep 21, 2018

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

@tkersten09

This comment has been minimized.

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

This comment has been minimized.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.