如何将文件夹从 git repo 链接到另一个 repo?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36554810/
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
How to link folder from a git repo to another repo?
提问by Manoj
I want to create a public repo to put some sample files from my main repo (private). Is there any way to soft link few folders from a git repo to another git repo?
我想创建一个公共存储库来放置我的主存储库(私有)中的一些示例文件。有没有办法将几个文件夹从一个 git 仓库软链接到另一个 git 仓库?
回答by CodeWizard
Then you should use submodules for this task.
那么你应该使用子模块来完成这个任务。
Submodule are different git repositories under the same root.
This way you can manage 2 different project at folder level inside the root repository
子模块是同一根下的不同 git 存储库。
通过这种方式,您可以在根存储库内的文件夹级别管理 2 个不同的项目
Submodules
allow foreign repositories to be embedded within a dedicated subdirectoryof the source tree, always pointed at a particular commit.
Submodules
允许将外部存储库嵌入源树的专用子目录中,始终指向特定提交。
git submodule
git submodule
Break your big project to sub projects as you did so far.
Now add each sub project to you main project using :
像到目前为止一样将您的大项目分解为子项目。
现在使用以下命令将每个子项目添加到您的主项目中:
git submodule add <url>
Once the project is added to your repo, you have to init and update it.
将项目添加到您的存储库后,您必须对其进行初始化和更新。
git submodule init
git submodule update
As of Git 1.8.2new option --remote
was added
从Git 1.8.2--remote
开始,添加了新选项
git submodule update --remote --merge
will fetch
the latest changes from upstream in each submodule, merge them in
, and check out
the latest revision of the submodule.
将fetch
每个子模块中上游的最新更改merge them in
,以及子模块check out
的最新版本。
As the docsdescribe it:
正如文档描述的那样:
--remote
This option is only valid for the update command. Instead of using the superproject's recorded SHA-1 to update the submodule, use the status of the submodule's remote-tracking branch.
--remote
此选项仅对更新命令有效。不要使用超级项目记录的 SHA-1 来更新子模块,而是使用子模块的远程跟踪分支的状态。
This is equivalent to running git pull in each submodule.
这相当于在每个子模块中运行 git pull 。
However, how would I push a commit in the scenario of bug fix in C which affects the code shared with the parent layers?
但是,在 C 中的错误修复方案中,我将如何推送提交,这会影响与父层共享的代码?
Again: using submodule will place your code inside your main project as part of its content. The difference between having it locally inside the folder or having it as part of a submodule is that in submodule the content is managed (commited) to a different standalone repository.
再次:使用子模块会将您的代码作为其内容的一部分放置在您的主项目中。将它放在本地文件夹中或将其作为子模块的一部分之间的区别在于,在子模块中,内容被管理(提交)到不同的独立存储库。
This is an illustration of submodule - project inside another project in which each project is a standalone project.
这是子模块 - 另一个项目中的项目的说明,其中每个项目都是一个独立的项目。
git subtree
git subtree
Git subtree allows you to insert any repository as a sub-directory of another one
Git子树允许您将任何存储库作为另一个存储库的子目录插入
Very similar to submodule
but the main difference is where your code is managed. In submodules the content is placed inside a separate repo and is managed there which allow you to clone it to many other repos as well.
非常相似,submodule
但主要区别在于代码的管理位置。在子模块中,内容被放置在一个单独的存储库中并在那里进行管理,这允许您将其克隆到许多其他存储库。
subtree
is managing the content as part of the root project and not in a separate project.
subtree
将内容作为根项目的一部分进行管理,而不是在单独的项目中。
Instead of writing down how to set it up and to understand how to use it you can simply read this excellent post which will explain it all.
无需写下如何设置它并了解如何使用它,您只需阅读这篇出色的文章,它将解释这一切。
https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/
https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/
回答by Hack5
Answering your X problem not your Y problem (xyproblem.info), you should notuse submodules for this task. You should create a .gitignore to exclude the secrets from VCS. Alternatively, you could make the code read the config files from outside the VCS directory, so you can keep them in ~/.config. Storing config files in a private repo is almost never the right way.
回答你的问题,X不是你的问题Ÿ(xyproblem.info),你应该不使用子模块完成这个任务。您应该创建一个 .gitignore 以从 VCS 中排除机密。或者,您可以让代码从 VCS 目录之外读取配置文件,以便将它们保存在 ~/.config 中。将配置文件存储在私有仓库中几乎从来都不是正确的方式。
P.S. the answer by CodeWizard answers the Y problem perfectly.
PS CodeWizard 的答案完美地回答了 Y 问题。