Sparse checkout in Git 1.7.0?

With the new sparse checkout feature in Git 1.7.0, is it possible to just get the contents of a subdirectory like how you can in SVN? I found this example, but it preserves the full directory structure. Imagine that I just wanted the contents of the ‘perl’ directory, without an actual directory named ‘perl’.

— EDIT —

  • Composer autoload-dev does not work
  • Git Clone: Not Working (old local repo push/pull works OK)
  • How to get older tag of a git repository in elixir via mix file
  • How to list branches that contain an equivalent commit
  • What leads to a situation where one has to resolve the exact same conflict multiple times during a rebase?
  • How do you keep a Perforce repo downstream of git updated?
  • Example:

    My git repository contains the following paths


    What I want is to be able to produce from the above repository this layout:


    However with the current sparse checkout feature, it seems like it is only possible to get


    which is NOT what I want.

  • Merge user-names in Git repository commits?
  • Which files/folder should be in version control for Sencha Touch 2
  • Several urls for a submodule?
  • Command to clear the Git Bash screen
  • Suggestions please for a home version control system
  • What to not include in Git repository?
  • 7 Solutions collect form web for “Sparse checkout in Git 1.7.0?”

    You still need to clone the whole repository, which will have all the files. You could use the --depth flag to only retrieve a limited amount of history.

    Once the repository is cloned, the read-tree trick limits your “view” of the repository to only those files or directories that are in the .git/info/sparse-checkout file.

    I wrote a quick script to help manage the sparseness, since at the moment it is a bit unfriendly:

    echo > .git/info/sparse-checkout
    for i in "$@"
        echo "$i" >> .git/info/sparse-checkout
    git read-tree -m -u HEAD

    If you save this script as into the path reported by calling git --exec-path, then you can run git sparse foo/ bar/ to only “checkout” the foo and bar directories, or git sparse '*' to get everything back again.

    The short answer is no. Git sees all files as a single unit.

    What I recommend is that you break down you repositories into logical chunks. A separate one for perl, images, and docs. If you also needed to maintain the uber repo style you can create a repo made up of Submodules.

    richq’s answer was close, but it missed a step. You need to explicitly enable sparse checkout:

    git config core.sparsecheckout true

    This blog post has all the steps outlined clearly:

    Now without plunging into detail about why would you want to do this, your problem can be (probably) easily solved by a symlink/shortcut.

    To answer the question – no, and with a meaningful reason. The whole history of the repo is downloaded even with a ‘sparse checkout’. To clarify why this is necessary – otherwise tracking renamed files would be a pain in the …neck. Imagine you move the file /repo_root/asd/file1.cpp to /repo_root/fgh/file1.cpp – now if you had only downloaded /repo_root/fgh deltas, you won’t know about file1.cpp. So this means you must download all deltas. But then you have a full repository; not just a folder cut of it, therefore just the /rero_root/fgh folder is not a repo itself. This might not sound important when you checkout, but when you commit, git might not know enough to work alright.

    Workaround: If you really want to, you can create a script that calls git-checkout in such a manner (for the sh shell, batch for windows should not be hard to produce):

    cd $2
    git-checkout $1
    cp -R $3/* $4
    cd $curDir

    Here the first argument is the branch to checkout, the second – the folder where the repo is currently present, the third – the subdir you want to really use, and the fourth – the location to which you want it copied.

    Warning: my shell skills are almost non-existent, so use this after testing. It should not be hard to recreate the reverse of this script, that copies back stuff, so that it can be committed to the repo.

    git filter-branch --subdirectory-filter is what you need, see Detach (move) subdirectory into separate Git repository.

    Here is a little bash script to do that.

    This will first make a working copy of the original repo, then filter branch using subdirectory filter to you get what you want.

    git clone --no-hardlinks $1 $2
    cd $2
    git filter-branch --subdirectory-filter $2 --prune-empty --tag-name-filter cat HEAD -- --all
    git reset --hard
    git remote rm origin
    refbak=$(git for-each-ref --format="%(refname)" refs/original/)
    if [ -n "$refbak" ];then
        echo -n $refbak | xargs -n 1 git update-ref -d
    git reflog expire --expire=now --all
    git repack -ad
    git gc --aggressive --prune=now

    Use for the example in the question, repo perl would work.

    You can try braid – it tracks remotes while matching them to a path.

    It appears what you are trying to do is rename the directory tree such that your files end up in a different place. It appears to me that what you are asking to do is an anti-template for code/project management on two counts: categorization of modules (java bits under java node, perl under perl node), and having a project with files in different locations from where the developer visualizes them. Since git maintains hashes of directory contents to see what is changed, this also breaks git as such.

    Daemeon Reiydelle

    Git Baby is a git and github fan, let's start git clone.