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

前端 未结 18 2884
时光说笑
时光说笑 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 05:07

    Just to clarify some of the great answers here, the steps outlined in many of the answers assume that you already have a remote repository somewhere.

    Given: an existing git repository, e.g. git@github.com:some-user/full-repo.git, with one or more directories that you wish to pull independently of the rest of the repo, e.g. directories named app1 and app2

    Assuming you have a git repository as the above...

    Then: you can run steps like the following to pull only specific directories from that larger repo:

    mkdir app1
    cd app1
    git init
    git remote add origin git@github.com:some-user/full-repo.git
    git config core.sparsecheckout true
    echo "app1/" >> .git/info/sparse-checkout
    git pull origin master
    

    I mistakenly thought that the sparse-checkout options had to be set on the original repository: this is not the case. You define which directories you want locally, prior to pulling from the remote. Hope this clarification helps someone else.

提交回复
热议问题