git - 使用 git 子树时如何添加特定文件夹?

标签 git git-subtree

我正在开发一个复杂的 Ionic 项目。我正在开发的许多组件和提供程序都是通用的,可以用于我公司正在进行的其他项目。这在软件开发中很常见。这是我想出的 Git 工作流程(此图显示分支):

my-company-library-repo ----
                            |_ component 1 feature branch
                               |_ company component 1 feature branch testbed
                            |_ component 2 feature branch

在测试平台中开发的最终组件代码(只是一个.ts.js 文件)被推送到组件功能分支。测试平台代码保留在测试平台分支中。此外,在功能分支中还有可能伴随该组件的任何文档。

现在在应用程序存储库中,我使用以下命令将功能分支添加为子树:

git subtree add -P <destination-dir/feature> --squash <my-company-library-repo-url> <feature-branch-name>

这给了我以下信息(此图显示文件夹结构):

my-app-repo-------
                  |_ company-library-feature-subtree

这应该只包含 .js.ts 及其子文件夹中的文档。我得到这个只是为了部分工作。当它 pull 子树时,它只 pull 组件和它的 doc 文件,但是文件被 pull 入一个很长的子目录列表,如下所示:

my-app-repo/src/feature-branch/feature/src/app/providers/...

这使得很难使用该库,因为文件放置在太多目录(未使用的目录)深处。

所以,当我将我的 2 个文件从 feature-testbed 分支推送到 feature 分支时,我怎么能不把整个目录结构和它们一起 pull 呢?

最佳答案

在将子树添加到 my-app-repo 之前, 从 my-company-library-repo 中拆分一棵子树:

# In my-company-library-repo
git subtree split -P src/app/providers/... -b feature-new feature

这将创建一个包含 src/app/providers/... 内容的新历史记录在 repo 的根目录,从 feature 开始分支,并创建分支 feature-new在这段历史的尽头。

然后将该新分支作为子树添加到 my-app-repo :

# In my-app-repo
git subtree add -P <destination-dir/feature> --squash <my-company-library-repo> feature-new

现在您将拥有 src/app/providers/... 的内容在 <destination-dir/feature> .

您没有提到是否会定期重复此过程,但这也是可能的。来自git-subtree手册页:

Repeated splits of exactly the same history are guaranteed to be identical (ie. to produce the same commit ids). Because of this, if you add new commits and then re-split, the new commits will be attached as commits on top of the history you generated last time, so 'git merge' and friends will work as expected.

关于git - 使用 git 子树时如何添加特定文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266785/

相关文章:

php - Git 从 github pull 只有 php 文件给出 500 内部服务器错误

git - 致命的 : Not a git repository: git status works. 但是 git 添加。没有

git子树分割 "No new revisions were found"

bash - 如何在没有 git 二进制文件的情况下获取当前的 git 标签?

git - 在 GitHub 中 rebase 后由 x 提交在后面由 y 提交

git - 致命的 : cannot create directory at/root/wp-content/cache/page_enhanced/No such a file or directory

git - 删除无效的 git-subtree-split 哈希

git - 为什么 git subtree 缺少提交?

git - 执行git subtree split时遵循renames

eclipse - 在 git 文件中跟踪 Eclipse 任务列表