Using Git Sparse Checkout

June 5, 2013

There are times when all I want or need from a Git repo are a handful files. For example, I use the awesome git-completion and git-prompt scripts included in the Git project. Thus, I don't need the entire Git repo taking up space.

This is exactly what sparse checkouts enable.

Here are the steps to create a "sparse" Git local repository that only includes the "Completion" scripts.

Step 1: Create a directory.

I named mine git-completion. You can name the directory whatever you want.

mkdir git-completion
cd git-completion

Step 2: Initialize a Git repository

git init

Step 3: Enable Sparse Checkouts

git config core.sparsecheckout true

Step 4: Tell Git which directories you want

echo contrib/completion/ >> .git/info/sparse-checkout

Or you can modify the .git/info/sparse-checkout file directly. Either way is fine.

Step 5: Add the remote

git remote add -f origin

Final Step: Fetch the files

git pull origin master

You should now have the contrib/completion directory. No other Git source files exist in your local copy.

Sourcing the files

Update your .bashrc file.

source ~/Development/git-completion/contrib/completion/git-completion.bash
source ~/Development/git-completion/contrib/completion/