Git 合并测试分支(最终提交)到主分支

发布于 2025-01-01 10:10:32 字数 178 浏览 1 评论 0原文

我创建了一个测试分支。它有很多微小的承诺来构建一项功能。 最后,我想获取最终完成的更改,并将它们放入主分支中。
主分支不应包含测试分支的历史记录。
测试分支最终将被删除。

实现这一目标的最佳方法是什么?
生成补丁并将其应用到 master 上是最好的方法吗?
如果是这样,我如何生成/应用补丁?

I have created a testing branch. It has a lot of tiny commits to build one feature.
At the end of it, I want to take the final completed changes, and put them into the master branch.
The master branch, shouldn't contain the history of the testing branch.
Testing branch will be removed eventually.

What is the best way to achieve this?
Would generating a patch and applying it on master be the best way?
If so, how do I generate/apply the patch?

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

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

发布评论

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

评论(3

舞袖。长 2025-01-08 10:10:32

了解 Git 工作流程”中描述了各种方法:

短暂的工作

绝大多数时候,我的清理工作只是挤压合并。

git checkout master
git merge --squash private_feature_branch
git commit -v

更大的工作

我决定我的改变应该被分解成更小的改变,所以壁球是一种太生硬的工具。 (根据经验,我会问:“这容易进行代码审查吗?”)

git rebase --interactive master

(在你的情况下不是一个选项,正如你在问题中提到的)

宣布分支机构破产

也许我的功能分支存在了很长一段时间,我必须将几个分支合并到我的功能分支中,以使其在工作时保持最新。历史错综复杂。
最简单的方法是获取原始差异并创建一个干净的分支。

git checkout master
git checkout -b cleaned_up_branch
git merge --squash private_feature_branch
git reset

我现在有一个工作目录,其中充满了我的更改,并且没有来自上一个分支的包袱。现在我手动添加并提交更改。

Various approaches are described in "Understanding the Git Workflow":

Short lived work

The vast majority of the time, my cleanup is just a squash merge.

git checkout master
git merge --squash private_feature_branch
git commit -v

Larger work

I decide my change should be broken into smaller changes, so squash is too blunt an instrument. (As a rule of thumb I ask, “Would this be easy to code review?”)

git rebase --interactive master

(not an option in your case, as you mention in your question)

Declaring Branch Bankruptcy

Maybe my feature branch existed for a very long time, and I had to merge several branches into my feature branch to keep it up to date while I work. History is convoluted.
It’s easiest to grab the raw diff create a clean branch.

git checkout master
git checkout -b cleaned_up_branch
git merge --squash private_feature_branch
git reset

I now have a working directory full of my changes and none of the baggage from the previous branch. Now I manually add and commit my changes.

余罪 2025-01-08 10:10:32

您正在寻找 git merge--squash 选项:

--squash --no-squash

生成工作树和索引状态,就像发生真正的合并一样
(合并信息除外),但实际上并不进行提交
或移动 HEAD,也不记录 $GIT_DIR/MERGE_HEAD 导致下一个 git
commit 命令用于创建合并提交。这允许您创建一个
当前分支顶部的单个提交,其效果与
合并另一个分支(如果是章鱼则合并更多分支)。

使用 --no-squash 执行合并并提交结果。这个选项
可以用来覆盖 --squash。

所以 git merge --squash test-branch 并提交。

您也可以尝试 git merge --no-commit --no-ff test-branch

You are looking for --squash option of git merge:

--squash --no-squash

Produce the working tree and index state as if a real merge happened
(except for the merge information), but do not actually make a commit
or move the HEAD, nor record $GIT_DIR/MERGE_HEAD to cause the next git
commit command to create a merge commit. This allows you to create a
single commit on top of the current branch whose effect is the same as
merging another branch (or more in case of an octopus).

With --no-squash perform the merge and commit the result. This option
can be used to override --squash.

So git merge --squash test-branch and commit away.

You may also try git merge --no-commit --no-ff test-branch

焚却相思 2025-01-08 10:10:32
git checkout master
git merge --squash testing-branch

--squash 会将您的工作集置于这样的状态,就好像测试分支上的所有更改都已合并到 master 中,但不会创建任何提交。然后,您可以执行 git commit ,所有更改都将在一次提交中进行。

git checkout master
git merge --squash testing-branch

That --squash will put your working set in the state as if all the changes on testing-branch got merged in with master, but won't create any commits. You can then do git commit and all the changes will go in in a single commit.

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