On Windows git: “error: Sparse checkout leaves no entry on the working directory”
Asked Answered
L

2

30

I had the error:

“error: Sparse checkout leaves no entry on the working directory”

when trying to set up a sparse checkout in git on windows. I've answered my own question after researching the issue and finding the full answer spread out across the web.

Leipzig answered 25/4, 2014 at 9:18 Comment(0)
L
67

tl;dr

From the folder in which you want the source to live, using a git-bash prompt NOT powershell (although see my edit below):

git clone –n <repo> <local-directory-name>
cd <local-directory-name>
git config core.sparsecheckout true
echo some/sub-folder/you/want >> .git/info/sparse-checkout
git checkout <branch-name>

Full explanation

Sparse checkouts enable you to work on a subset of the repository. It’s worth the effort to set up sparse checkouts on large repositories, everything is much faster!

I struggled with setting it up on windows getting a lot of “error: Sparse checkout leaves no entry on the working directory”. After a bit of research, I found the following steps were minimal and had consistently good results for me – YMMV.

The most important point is this – DO NOT USE POWERSHELL/CMD FOR THESE STEPS – use a git bash prompt. This is because otherwise the echo command produces a UNICODE file with a BOM marker. The file MUST be an ANSI formatted file with UNIX style line endings for git to parse it correctly. This is the most likely cause of “error: Sparse checkout leaves no entry on the working directory” errors.

  1. From a parent of the directory you wish to be the root folder, assuming a folder name of “src” do the following, This grabs the whole repo (which is still required), but does importantly does not perform a checkout, and correctly sets up the origin remote and branch tracking.

    git clone –n <your repository> src
    
  2. You should have a fairly empty looking src folder (it will have the .git hidden folder). Now run the following. This updates the local git config to turn on sparse checkouts, and creates a file in the .git/info hidden folder that will list all the folders you want to include when doing a sparse checkout. This can be edited later to add other folders. Important: Be sure to use a git bash prompt for the below, and use an editor supporting unix line-ending conventions and save as ANSI when editing the sparse-checkout file, or you may get “error: Sparse checkout leaves no entry on the working directory” errors. The <path you want to checkout> might look like this: path/to/subfolder - note the lack of leading and trailing path separators.

    cd src
    git config core.sparsecheckout true
    echo <path you want to checkout> >> .git/info/sparse-checkout
    
  3. Now you are ready to checkout. This should leave just the sparse-checkout folders populated. If it doesn’t, carefully check your spelling of everything above!

    git checkout <branch-name>
    

Edit - Echo in Powershell

I recently discovered that echo in powershell is a synomym for write-output. It's possible to get ASCII output by piping the echo to the out-file command which allows you to specify an encoding. So this will work from Powershell and give the correct file contents:

echo some/sub/folder/you/want | out-file -encoding ascii .git/info/sparse-checkout
Leipzig answered 25/4, 2014 at 9:18 Comment(6)
Never trust anything with the phrase Power in it.Bechtel
Another solution is to use the echo command that comes with the git client. This is what worked for me: %GITHOME%\usr\bin\echo.exe <path you want to checkout> >> .git/info/sparse-checkoutHedgepeth
This will still result in the "want" folder being created. Is there a way to prevent that? I have tried with 'path/you/want/*' but the same issue occurs.Ailurophobe
Not clear what you are after. The "want" folder being created is the intention. Sparse checkouts filter the checkouts, they don't relocate them.Leipzig
More reliable than Out-File -encoding is replacing it with Set-Content -Encoding Ascii . The out file version includes invoking formatters, which may give you unexpected results. Note that Powershell is the new, crossplatform version. Many comments are people are using Windows Powershell which has tons of encoding edge cases that the new one does not. <learn.microsoft.com/en-us/powershell/module/…>Reprimand
I would have thought you're unlikely to hit any of those encoding edge cases with a typical file path though.Leipzig
E
3

The marked answer is correct, but you do not need to use "git bash" on Windows. It is easy to do the same in PowerShell:

Set-Content .git\info\sparse-checkout "some/sub-folder/you/want/*" -Encoding Ascii

This will create a file in ANSI encoding (obviously), but with CR LF (Windows) line endings. That seemed to be no problem for git though, it worked fine and did a sparse checkout.

Tested on Windows Server 2012.

Ewart answered 14/3, 2019 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.