git:将一个分支源树的一部分合并到另一个分支中,无论单独提交如何

发布于 2024-08-11 07:13:56 字数 493 浏览 7 评论 0原文

我了解如何在 git 中将分支合并在一起,而且我喜欢它的简单性。

我有一个看起来像这样的分支:

project/
|--subproj1/
|  |---(files)
|
|--subproj2/
   |---(files)

一位同事有一个对两个子项目都进行了重大更改的分支。更具体地说,他对它们进行了更改,使其依赖于新 subproj3 中的公共代码位。然而,由于各种原因,我们在可预见的将来不会推动他的分支。

我的分支仅在 subproj1 中与 master 分支不同,我正在其中进行特定于子项目的增强。我的子项目不如其他一些子项目重要,因此对我们可以推动的更改类型的限制较少。我想合并我同事的 subproj1 更改(以及这些更改所依赖的他的新 subproj3),但保留我现有的 subproj2 。将来的某个时候,当他将他的项目完全合并到 master 时,我希望他的 subproj2 更改能够正常合并。

我如何使用 git 做到这一点?

I understand how to merge branches together in git, and I love how easy it makes it.

I have a branch that looks like this:

project/
|--subproj1/
|  |---(files)
|
|--subproj2/
   |---(files)

A colleague has a branch that's made significant changes to both subprojects. More specifically, he's altered them both to depend on a common bit of code in a new subproj3. However, for various reasons, we're not pushing his branch anytime in the forseeable future.

My branch diverges from master only in subproj1, where I'm making subproject-specific enhancements. My subproject is less important than some of the others, and thus has less constraints on what types of changes we can push on it. I'd like to merge in my colleague's subproj1 changes (and his new subproj3 which those changes depend on) but leave my existing subproj2 alone. Sometime in the future, when he merges his project in entirety to master, I'd like his subproj2 changes to merge in normally.

How do I do this with git?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2024-08-18 07:13:56

每次提交都是一个逻辑工作单元。不幸的是,这意味着如果您的同事在同一分支上对 subproj1 和 subproj2 进行了编辑,则您无法将其分开。

但是,要执行您所要求的操作,您可以使用 git filter-branch 。您可以使用它来创建一个新分支,该分支是通过重播所有同事的提交来定义的,但仅将对 subproj1 的更改添加到每个提交。

这些命令大致如下:(

git fetch colleague-repo ...
git filter-branch --subdirectory-filter subproj1 -- A..B

其中 A..B 是您从他的存储库中获取的提交范围)

通过这样做,您将创建一个独特的分支,其工作与您同事的工作完全分开。如果您将这个新分支和您同事的分支合并到 master 中,则 subproj1 的更改历史记录将永久分配在两个分支之间。

Each commit is a logical unit of work. Unfortunately, that means you cannot separate the edits you colleague has made to subproj1 and subproj2 if he made them on the same branch.

However, to do what you are asking, you would use git filter-branch. You can use this to create a new branch which is defined by replaying all of your colleague's commits but only adding changes to subproj1 to each commit.

The commands are something along the lines of:

git fetch colleague-repo ...
git filter-branch --subdirectory-filter subproj1 -- A..B

(where A..B is the range of commits you fetch from his repository)

By doing this you are creating a unique branch with work that is totally separate from your colleague's work. If you merge this new branch and your colleague's branch into master, the history of changes to subproj1 will permanently be split between the two branches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文