Cloning only a subdirectory with git [duplicate]

…衆ロ難τιáo~ 提交于 2019-11-27 10:18:22

问题


This question already has an answer here:

  • How do I clone a subdirectory only of a Git repository? 13 answers

I have a git repository that includes subdirectories. Example dirA/dirB. Is there any way to do a git clone on a Unix server to pull only files from a subdirectory (dirB)?

Is there some other git command other than clone that will do this?

How do I git a specific file?


回答1:


If you want to clone only parts of your repository, you should look at git submodules.

The subdirectory is managed by a specific git repository and is referenced in the main repository as a submodule.

When you clone the main repository, the submodules are not automatically cloned.




回答2:


Suppose your project is in a dir called project, and you want only those commits which touch project/dirB.

Then:

git clone project/ subproject/
cd subproject
git filter-branch --prune-empty --subdirectory-filter dirB HEAD 

subproject will now contain the git history which touches dirB.




回答3:


Cf https://www.kernel.org/pub/software/scm/git/docs/git-archive.html

With a shell command :

git archive --remote=<repo_url> <branch> <path> | tar xvf -



回答4:


I would like to share a workaround. You can clone the entire repo, and then create a symulink for subdirectory:

mkdir <repo_dir>
cd <repo_dir>
git clone <repo_url>
ln -s <sub_dir> <your_location_where_you_want_to_checkout_sub_dir>

Again, this is not a solution - just a workaround. You will still clone the whole repo, and you can pull the changes only from . However, this was useful in some case of mine.




回答5:


It's not really cloning, but if you just want a part of a project to start a new repo with, go into your repo and

git archive master <subdir> | tar -x -C /somewhere/else

Then go /somewhere/else and start a new repo there.




回答6:


Git works on the repository level. You will not be able to select only a subdirectory. If the repository that you are interested in has a web interface, you might be able to navigate to your subdirectory and grab it.



来源:https://stackoverflow.com/questions/11834386/cloning-only-a-subdirectory-with-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!