I am trying to understand how (and if it is possible) to copy only specific files of a specific directory from remote
in Git. (not interested in continuing working on those files or getting the history)
For example, say the remote master branch holds (among many others) a directory with the name \src
into which there are .cpp
and .h
files. How would it be possible to only get a copy of all the header .h
files?
Of course, I am looking if an approach exists that would not lead to re-writing the repo or any other undesired side effects. Simply, getting a local copy of only some specified files from the remote.
I have considered git archive and sparse-checkout but could not understand if I can use them to achieve my goal.
There are two kinds of subsets you can specifiy during git clone
:
-b branchname --single-branch
allows you to only get those parts of the history that directly lead up to branchname
. It will skip everything not necessary to fully describe branchname
.--depth n
allows you to truncate history beyond a certain depth.Aside from that, clone
(as well as commit
, push
, pull
, merge
) and so on always by design process the whole directory tree starting at the root. The commit
(object) is the item they work with, they do not know about individual files (as opposed to CVS or SubVersion, for example, which can and regularly do work on individual files).
There are some ways to work with specific files/directories, but those are rather lowlevel and probably not what you want. For example, you can use the git protocol to fetch individual git objects (commits, trees, blobs...) directly... but I feel that's not what you are asking for.
Update: if you want to just grab the file and ignore the history, then you can use --depth 1
(to get the whole directory tree, but just for a single commit), grab your files, and be done with it. You still would need to download a lot more than you need, but at least it will be magnitudes less than the whole history (for something large like the Linux kernel).
Update 2: I am pretty sure that, except for doing your own implementation of the git networking protocol, there is no way to fetch single bits and pieces from a remote with the git commands. If you talk the protocol yourself, it's (relatively) easy of course. http://git-scm.com/book/en/v2/Git-Internals-Transfer-Protocols describes it, and it does not seem outlandish hard to use it yourself.