只用 git 克隆一个子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11834386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cloning only a subdirectory with git
提问by user1526912
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)?
我有一个包含子目录的 git 存储库。示例 dirA/dirB。有没有办法git clone
在 Unix 服务器上执行 a来仅从子目录(dirB)中提取文件?
Is there some other git command other than clone that will do this?
除了 clone 之外,还有其他一些 git 命令可以执行此操作吗?
How do I git a specific file?
我如何git一个特定的文件?
采纳答案by Benoit Courtine
If you want to clone only parts of your repository, you should look at git submodules.
如果您只想克隆存储库的一部分,您应该查看git submodules。
The subdirectory is managed by a specific git repository and is referenced in the main repository as a submodule.
子目录由特定的 git 存储库管理,并在主存储库中作为子模块引用。
When you clone the main repository, the submodules are not automatically cloned.
克隆主存储库时,不会自动克隆子模块。
回答by unutbu
Suppose your project is in a dir called project
, and you want only those commits which touch project/dirB
.
假设您的项目位于名为 的目录中project
,并且您只需要那些 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
.
subproject
现在将包含触及 .git 历史记录dirB
。
回答by Bruno Thomas
Cf https://www.kernel.org/pub/software/scm/git/docs/git-archive.html
参见https://www.kernel.org/pub/software/scm/git/docs/git-archive.html
With a shell command :
使用 shell 命令:
git archive --remote=<repo_url> <branch> <path> | tar xvf -
回答by Darko Vasilev
I would like to share a workaround. You can clone the entire repo, and then create a symulink for subdirectory:
我想分享一个解决方法。您可以克隆整个 repo,然后为子目录创建一个符号链接:
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.
同样,这不是解决方案 - 只是一种解决方法。您仍将克隆整个存储库,并且只能从 . 但是,这在我的某些情况下很有用。
回答by commonpike
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.
然后去 /somewhere/else 并在那里开始一个新的回购。
回答by Nicolas Dudebout
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.
Git 在存储库级别工作。您将无法仅选择子目录。如果您感兴趣的存储库具有 Web 界面,您或许可以导航到您的子目录并获取它。