Git: How do I clone a subdirectory only of a Git repository?

前端 未结 18 2954
时光说笑
时光说笑 2020-11-21 04:33

I have my Git repository which, at the root, has two sub directories:

/finisht
/static

When this was in SVN, /finisht was chec

相关标签:
18条回答
  • 2020-11-21 04:58

    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.

    0 讨论(0)
  • 2020-11-21 04:59

    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.

    0 讨论(0)
  • 2020-11-21 05:02

    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
    
    0 讨论(0)
  • 2020-11-21 05:03

    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.

    0 讨论(0)
  • 2020-11-21 05:05

    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.

    0 讨论(0)
  • 2020-11-21 05:06

    It's not possible to clone subdirectory only with Git, but below are few workarounds.

    Filter branch

    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.


    Sparse checkout

    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.

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

    2. Enable sparseCheckout option:

      git config core.sparseCheckout true
      
    3. 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.

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


    0 讨论(0)
提交回复
热议问题