I have my Git repository which, at the root, has two sub directories:
/finisht
/static
When this was in SVN, /finisht
was chec
Git 1.7.0 has “sparse checkouts”. See “core.sparseCheckout” in the git config manpage, “Sparse checkout” in the git read-tree manpage, and “Skip-worktree bit” in the git update-index manpage.
The interface is not as convenient as SVN’s (e.g. there is no way to make a sparse checkout at the time of an initial clone), but the base functionality upon which simpler interfaces could be built is now available.
If you're actually ony interested in the latest revision files of a directory, Github lets you download a repository as Zip file, which does not contain history. So downloading is very much faster.
Using Linux? And only want easy to access and clean working tree ? without bothering rest of code on your machine. try symlinks!
git clone https://github.com:{user}/{repo}.git ~/my-project
ln -s ~/my-project/my-subfolder ~/Desktop/my-subfolder
Test
cd ~/Desktop/my-subfolder
git status
If you never plan to interact with the repository from which you cloned, you can do a full git clone and rewrite your repository using git filter-branch --subdirectory-filter. This way, at least the history will be preserved.
For other users who just want to download a file/folder from github, simply use:
svn export <repo>/trunk/<folder>
e.g.
svn export https://github.com/lodash/lodash.com/trunk/docs
(yes, that's svn here. apparently in 2016 you still need svn to simply download some github files)
Courtesy: Download a single folder or directory from a GitHub repo
Important - Make sure you update the github URL and replace /tree/master/
with '/trunk/'.
As bash script:
git-download(){
folder=${@/tree\/master/trunk}
folder=${folder/blob\/master/trunk}
svn export $folder
}
Note This method downloads a folder, does not clone/checkout it. You can't push changes back to the repository. On the other hand - this results in smaller download compared to sparse checkout or shallow checkout.
It's not possible to clone subdirectory only with Git, but below are few workarounds.
You may want to rewrite the repository to look as if trunk/public_html/
had been its project root, and discard all other history (using filter-branch), try on already checkout branch:
git filter-branch --subdirectory-filter trunk/public_html -- --all
Notes: The --
that separates filter-branch options from revision options, and the --all
to rewrite all branches and tags. All information including original commit times or merge information will be preserved. This command honors .git/info/grafts
file and refs in the refs/replace/
namespace, so if you have any grafts or replacement refs
defined, running this command will make them permanent.
Warning! The rewritten history will have different object names for all the objects and will not converge with the original branch. You will not be able to easily push and distribute the rewritten branch on top of the original branch. Please do not use this command if you do not know the full implications, and avoid using it anyway, if a simple single commit would suffice to fix your problem.
Here are simple steps with sparse checkout approach which will populate the working directory sparsely, so you can tell Git which folder(s) or file(s) in the working directory are worth checking out.
Clone repository as usual (--no-checkout
is optional):
git clone --no-checkout git@foo/bar.git
cd bar
You may skip this step, if you've your repository already cloned.
Hint: For large repos, consider shallow clone (--depth 1
) to checkout only latest revision or/and --single-branch
only.
Enable sparseCheckout
option:
git config core.sparseCheckout true
Specify folder(s) for sparse checkout (without space at the end):
echo "trunk/public_html/*"> .git/info/sparse-checkout
or edit .git/info/sparse-checkout
.
Checkout the branch (e.g. master
):
git checkout master
Now you should have selected folders in your current directory.
You may consider symbolic links if you've too many levels of directories or filtering branch instead.