I have my Git repository which, at the root, has two sub directories:
/finisht
/static
When this was in SVN, /finisht
was chec
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.