git - 单个文件作为 Git 子模块

标签 git git-submodules

我正在尝试确定 Git 存储库之间共享代码的最佳实践。

到目前为止,我显然遇到过看起来它们——几乎——符合要求的子模块。我的项目是一个结构简单的PHP MVC框架:

  • /应用
  • 核心.php
  • /核心

app 是一个文件夹,包含特定于应用程序的 Controller 、模型、 View 等,而 core 包含通用的,例如登录 Controller 。 core.php 文件本身是所有请求的全局处理程序。

因此,我所有部署的这个 MVC 框架的共享代码是 core.phpcore

我可以看到如何将 core 变成 Git 子模块,而不是 core.php

这可能吗?我是否需要重新构建我的框架,以便 core.php 驻留在 core 文件夹中,这样我就可以将整个文件夹作为一个子模块,或者有更好的方法吗?

最佳答案

如果您可以使用符号链接(symbolic link)(例如,您没有使用 Windows),那么您可以像这样设置 corecore.php:

# "base" repository layout:
core/
core.app

# each app repository layout:
base/
  core/
  core.php
core -> base/core/
core.php -> base/core.php
app/

在每个应用程序存储库中,base/ 目录是使用“base”存储库的子模块或“base”存储库的子树 merge 。

这两种方法都可以让您开始在特定应用程序的上下文中更改基本代码,然后将这些更改 pull 回主基本存储库。当使用子模块时,你必须小心在发布任何引用这些新基础提交的应用程序提交之前总是发布新的基础提交(这在使用子树 merge 时不是问题,因为每个应用程序都是“扁平的”并且有效地拥有自己的副本基地)。

第三方git subtree如果您决定不使用子模块,命令似乎是管理子树 merge 的一种非常好的方式。

子树

git init newapp
cd newapp
ln -s base/core
ln -s base/core.php
git add core core.php
git commit -m'point to base (to be added next)'

# hook up base
git subtree add --prefix=base git@git.example.com:me/app_base.git master

mkdir app
# edit app/bar.php

# update base
git subtree pull --prefix=base git@git.example.com:me/app_base.git master

.
|-- .git/
|   |-- ...
|   `-- ...
|-- app/
|   `-- bar.php
|-- base/
|   |-- core/
|   |   `-- foo.php
|   `-- core.php
|-- core -> base/core/
`-- core.php -> base/core.php

子模块

git init newapp
cd newapp
ln -s base/core
ln -s base/core.php
git add core core.php
git commit -m'point to base (to be added next)'

# hook up "base"
git submodule add git@git.example.com:me/app_base.git base
git commit -m'incorporate base'

mkdir app
# edit app/bar.php

# update base
(cd base && git fetch origin && git merge origin/master)
git add base
git commit -m'updated base'
.
|-- .git/
|   |-- ...
|   `-- ...
|-- .gitmodules
|-- app/
|   `-- bar.php
|-- base/
|   |-- .git/
|   |   |-- ...
|   |   `-- ...
|   |-- core/
|   |   `-- foo.php
|   `-- core.php
|-- core -> base/core/
`-- core.php -> base/core.php

关于git - 单个文件作为 Git 子模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170930/

相关文章:

git - 如何更改 git 子模块的 HEAD

git - 点文件存储库 : Switching from per-package Git submodules to ELPA while maintaining portability

git - 如何启用我的 Azure 管道以使用 Git checkout 子模块?

git - 如何将 git 中的子模块更新为该外部仓库中的 HEAD 提交

git - 恢复推送的 git 提交

Git SVN 区分大小写的文件夹名称

git - 安装 TortoiseGIT 时无法选择 SSH 客户端

git - 如何在 git 中配置我的代码编辑器?

git - 什么是 'dirty' 子模块?

git - 如何设置子模块 Azure DevOps