9

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
2
  • Are you simply trying to extract the files from the git repository? Do you want to still be able to track changes and commit them? Commented Aug 22, 2014 at 1:50
  • If I recall, I wanted to still track and commit. Commented Aug 23, 2014 at 6:02

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Would I be right in thinking this is like a git version of a symlink? This definitely works, but repeating for each file is highly inconvenient if there are a lot of files in the sub-folder (and more directories too).
No, it's basically just retrieving the content of a file from the repository and storing it somewhere else. In fact, if you do git status it will show it as a new untracked file, and if you do git add, it will consider it a copy/rename from the original path.
3
+50

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.