Question

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
Was it helpful?

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...

OTHER TIPS

Depending on what you are trying to accomplish you have two options. If you want to be able to track changes to the files still, then I would commit symlinks. If you don't care about local changes, then I would use git archive.

Commit symlinks

You can commit symlinks to the files that you care about, use spare checkout to get those as well.

ln -s contrib/completion/git-completion.bash git-completion.bash
git add git-completion.bash

This will allow you to edit the files and commit changes to them still.

Git archive

This will simply extract the content you are looking for. Changes will not be tracked in the repository.

# generic
git archive <revision>:<path> | tar -xf -

#specific to your example
git clone --bare https://github.com/git/git.git ~/git-completion-bare
mkdir git-completion && cd git-completion
git archive --remote ~/git-completion-bare master:contrib/completion/ | tar -xf -

Edit: It looks like github does not support git archive --remote. It is still included below in case someone is using a different hosting service that allows it.

Git archive with --remote

Using git archive, you can even download the changes directly from a remote repository without cloning the repository first. The downside is you cannot track local changes with Git.

# General form
git archive --remote <url> <revision>:<path> | tar -xf -
# your example 
git archive --remote https://github.com/git/git.git master:contrib/completion/ | tar -xf -

You will now have the files in the current directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top