如何更改 git 子模块以指向子文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5303496/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:21:19  来源:igfitidea点击:

How to change a git submodule to point to a subfolder?

gitgit-submodules

提问by ripper234

Skimming through the SubModule tutorial, I created a submodule out of the boto project. Then, I discovered that I actually need only a subset of this project - specifically, the botofolder.

浏览SubModule 教程,我从boto 项目创建了一个子模块。然后,我发现我实际上只需要这个项目的一个子集——特别是boto文件夹。

I would like to change my submodule to point to this folder. When I look in .gitmodules, I see

我想更改我的子模块以指向此文件夹。当我查看 .gitmodules 时,我看到

[submodule "backup/src/boto"]
    path = backup/src/boto
    url = https://github.com/boto/boto.git

What URL should I use instead of https://github.com/boto/boto.git? After I change the URL, should I delete the boto folder locally and re-pull?

我应该使用什么 URL 而不是https://github.com/boto/boto.git?更改URL后,是否应该在本地删除boto文件夹并重新拉?

采纳答案by Mark Longair

I'm afraid the URL for submodules always just points to the repository - you can't specify that you only want a subfolder of a repository, in the same way that git doesn't support "narrow clones" in general.

恐怕子模块的 URL 总是只指向存储库 - 您不能指定只想要存储库的子文件夹,就像 git 通常不支持“窄克隆”一样。

If you can't live with having the whole repository as a submodule, you could always create a new repository that's cloned from boto and then set up a cron job to:

如果您无法忍受将整个存储库作为子模块,您始终可以创建一个从 boto 克隆的新存储库,然后设置一个 cron 作业:

  1. git fetchthat repository into a directory
  2. Use git filter-branchto update a branch where the subdirectory is at the top level.
  3. Add that branch of the repository as the submodule. However, that's all a bit fiddly and my preference would just be to live with having the whole repository as a submodule.
  1. git fetch将该存储库放入一个目录中
  2. 使用git filter-branch其中的子目录是在顶层更新某个分支。
  3. 将存储库的该分支添加为子模块。然而,这有点繁琐,我更喜欢将整个存储库作为子模块。

回答by Artefact2

You cannot clone only a part of a repository. This is because git treats the repository as a whole object : when you get it, you get it all.

您不能只克隆存储库的一部分。这是因为混帐对待库作为一个整体对象:当你得到它,你就会得到它所有

So, the solution here would be to fetch the submodule in another directory, then use a symlink to achieve your goal.

因此,这里的解决方案是在另一个目录中获取子模块,然后使用符号链接来实现您的目标。

回答by Adam Dymitruk

What you want to do is create a branch in the submodule and move the folder up and delete what you don't need. You can then manage that branch instead. If you want to push up your changes, you should be able to back merge first. Git will know that you moved the files and do the merge successfully.

您想要做的是在子模块中创建一个分支并将文件夹向上移动并删除您不需要的内容。然后,您可以改为管理该分支。如果您想推动您的更改,您应该能够首先返回合并。Git 会知道您移动了文件并成功进行了合并。

Hope this helps.

希望这可以帮助。