Search code examples
gitsparse-checkout

Sparse Git checkout of files into parent directory


Borrowing from a few tutorials, I am able to do a sparse checkout of a particular folder in a git repo.

mkdir git-completion && cd git-completion
git init
git remote add –f origin https://github.com/git/git.git
git config core.sparsecheckout true
echo contrib/completion/ >> .git/info/sparse-checkout
git pull origin master

For example, let's say that I am after the git-completion.bash, git-completion.tcsh, etc scripts that are in the contrib/completion folder of this repo.

The above sparse-checkout does return me the desired folder, but it seems to have a nested file strucure:

git-completion/contrib/completion/git-completion.bash

Is it possible to pull the files instead into the parent directory like so?

git-completion/git-completion.bash

Solution

  • You can do this with git show:

    git show <revision>:path/to/file.txt > different/path/to/otherfile.txt
    

    In your specific case:

    git show HEAD:git-completion/contrib/completion/git-completion.bash > git-completion/git-completion.bash
    

    Repeat for each file you want to checkout. Although, really, the reason for the subdirectories, I think, is that when these get bundled into a git release, they're put in the contrib/completion subdirectory, so it might just be better to get used to the repository layout...