GIT:只从远程存储库添加一些文件作为子模块

时间:2014-01-08 02:37:15

标签: git

我需要在我的项目中添加一些JS库,我也希望能够从它的repo更新该库(这就是为什么我将它作为子模块添加)。但我只需要从该repo的1个文件放置到我的JS目录,而库存储库包含测试,coffeescript源等,我在我的存储库中不需要。所以问题是 - 如何将不是整个repo添加为子模块,而是添加单个文件/目录?

我想在Git Sparse checkouts的帮助下,这可能是某种可能的,但不确定我是否正确地使用了这个......

2 个答案:

答案 0 :(得分:4)

这是可能的,我已经多次完成了。

您需要做的就是创建一个您要添加为子模块的存储库分支,并删除您不想要的所有代码,并添加具有该分支的子模块。

例如,请查看此repository

答案 1 :(得分:3)

要添加到Abizernanswer(upvoted),您可以将您的子模块(设置为包含所需文件的分支)声明为 follow that branch git submodule man page):

git submodule add -b yourSubmoduleBranch /url/to/your/submodule/repo

如果您已经添加了子模块而没有-b,则可以轻松添加该选项:请参阅“Git submodules: Specify a branch/tag”。

# Make sure your submodule is actually at the latest of that branch:
cd path/to/your/submodule
git checkout -b branch --track origin/branch
# record that on the parent repo
cd /path/to/your/parent/repo
git config -f .gitmodules submodule.<path>.branch <branch>
git add yourSubmodule
git commit -m "Submodule now follows branch"

现在,在父级回购级别上,您需要为子模块做的所有操作都能反映您的最新修改:

git submodule update --init --remote --recursive
相关问题