How do I checkout a sub directory in a huge git repo with specified branch and with sparse checkout?

Viewed 3157

for example, I want to get this folder https://github.com/python/cpython/tree/2.7/Tools/freeze

The command I ran was:

mkdir python
cd python
git init
git remote add origin https://github.com/python/cpython.git
git config core.sparsecheckout true
echo "Tools/freeze/*" >> .git/info/sparse-checkout

# find remote branches
git remote show origin

# this works and pulls only that folder
git pull --depth=1 origin master

# but this doesn't, why?
git pull --depth=1 origin 2.7

# but how do I switch to remote 2.7 branch?
git checkout --track -b 2.7 origin/2.7
fatal: Cannot update paths and switch to branch '2.7' at the same time.
Did you intend to checkout 'origin/2.7' which can not be resolved as commit?

I read somewhere I need to run a git fetch before checkout, but it kind of defeat the purpose of sparse checkout, my internet is slow and repo is huge. How can I just get that subdirectory with branch 2.7 ? Thanks!

This is on windows8 and git bash

edit: if I ran git pull --depth=1 origin 2.7 it will pull remote 2.7 branch but it also brings every other files into my working directory, while if I ran git pull --depth=1 origin master, it only brings Tools/freeze directory in master branch ? why is this happening ?

another example:

mkdir qt
cd qt
git init
git remote add origin https://github.com/qtproject/qt.git
git config core.sparsecheckout true
echo util/qlalr/examples/lambda/* >> .git/info/sparse-checkout
git pull --depth=1 origin 4.8

That folder util/qlalr/examples/lambda is very small, but when it run the last command, it is still slow, can this be avoided ?

edit2: I realized that this is not possible with current git. but my only left question now is why git pull --depth=1 origin 2.7 doesn't respect sparse checkout config?

5 Answers

You have to create a local branch for reference. Updated steps should be:

git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "finisht/*" >> .git/info/sparse-checkout
git branch -b <your branch>
git pull --depth=1 origin <your branch>

I want to download particular folder from a branch without downloading the whole history of Master repository as that repo has huge amount of history and number of branches

mkdir testFolder
cd testFolder
git init
git remote add origin <URL>

Below git command will fetch branches starting with f_2, example: f_23, f_24 etc.

git config remote.origin.fetch +refs/heads/f_2*:refs/remotes/origin/f_2*
git fetch --depth=1

Set the Folder name which you want to checkout.

git sparse-checkout set <folderName>

Below command will download that particular folder from f_23 branch

git checkout f_23
Related