简体   繁体   English

Git:我有一个80多次提交的功能分支。如何在不破坏历史的情况下将其安全地合并到开发/生产中?

[英]Git: I've a feature branch with 80+ commits. How can I safely merge it into develop/production without ruining the history?

I've been working on a feature branch for a few weeks, and have a history of about 80 commits involving work across many sections of our project. 我已经在一个功能部门工作了几个星期,并且有大约80个提交的历史,涉及我们项目的许多部分的工作。 For small features I would just rebase, squash the commits into one succinct commit, merge it to develop, and push it. 对于小功能,我只需要重新定义,将提交压缩成一个简洁的提交,合并它以进行开发,然后推送它。 But here I've accumulated a long history with commits touching many different parts. 但在这里,我已经积累了很长的历史,承诺涉及许多不同的部分。

I'd like to split them into smaller commits, ie "Finished portion A of Feature F" "Finished portion B".. etc, but the work was not linear and each piece was built up at the same time. 我想将它们拆分成较小的提交,即“功能F的完成部分A”“完成部分B”等等,但是工作不是线性的,并且每个部分都是同时建立的。

I could squash them all into one commit and push it, but I'd lose all the history and it might make debugging in the future a pain because it's such a large change. 我可以将它们全部压缩成一个提交并推送它,但是我会失去所有的历史记录,它可能会使调试在未来变得很痛苦,因为它是如此大的变化。

I'm new to using git in a professional environment so I'm unsure of the best practices. 我不熟悉在专业环境中使用git,所以我不确定最佳实践。 What would your approach be to this situation? 你对这种情况有什么看法?

The general aproach is 一般的方法是

  1. Split the mixed feature commits 拆分混合功能提交
  2. Reorder the commits 重新排序提交

I assume that your repository looks like this 我假设您的存储库看起来像这样

o--------o----------o----------o
A        B          C          D

and that your feature changes are mixed and distributed 并且您的功能更改是混合和分发的

- feature 1 changes are in A,B,C
- feature 2 changes are in A,C,D
- feature 3 changes are in B,C,D

Split the commits 拆分提交

If you want to bring the changes for every feature in line you must first split the commits. 如果要对每个要素进行更改,必须首先拆分提交。

$ git rebase -i HEAD~3

which will open an editor 这将打开一个编辑器

pick A ...
pick B ...
pick C ...
pick D ...

Since you need to edit every commit change it to 由于您需要编辑每个提交更改它

edit A ...
edit B ...
edit C ...
edit D ...

Save and quit the editor and git will start to rebase. 保存并退出编辑器,git将开始rebase。 Git will rebase A and pause to let you do the edit. Git会重新定义A并暂停让你进行编辑。 Now you can reset commit A and make new commits in order to reflect every feature change. 现在,您可以重置提交A并进行新提交,以反映每个功能更改。

$ git reset HEAD~
$ git add ... # only files of feature A
$ git commit -m 'Feature A'
$ git add ... # only files of feature B
$ git commit -m 'Feature B'
$ git add ... # only files of feature C
$ git commit -m 'Feature B'
$ git rebase --continue

Git continues and you must repeat the above example again and again until all commits are splitted up. Git继续,您必须一次又一次地重复上述示例,直到所有提交被拆分为止。 Your repository than looks like 您的存储库看起来不像

o---o---o---o---o---o---o---o---o
E   F   G   H   I   J   K   L   M

See also Break a previous commit into multiple commits 另请参见将先前的提交分解为多个提交

Reorder the commits 重新排序提交

For now you have all changes to one feature in a single commit. 目前,您只需在一次提交中对一个功能进行所有更改。 But they are not in a linar order: 但它们不是一个线性顺序:

  • feature 1 changes are in E, G, I 特征1的变化在E,G,I中
  • feature 2 changes are in F, J, L 特征2的变化在F,J,L中
  • feature 3 changes are in H, K, M 特征3的变化是H,K,M

Now you can do another rebase -i and reorder the commits. 现在你可以做另一个rebase -i并重新排序提交。

pick E
pick G
pick I
pick F
pick J
pick L
pick H
pick K
pick M

Save and quit the rebase interactive editor and git will reorder the commits. 保存并退出rebase交互式编辑器,git将重新排序提交。 It might be possible that you get merge conflicts that you must solve. 您可能会遇到必须解决的合并冲突。 The commits will be reordered and therefore they might conflict with the prevoious commit now. 提交将被重新排序,因此它们可能与现在的早期提交冲突。

Hopefully git rebase completes without any conflict and you will end up with this repository 希望git rebase完成没有任何冲突,你最终会得到这个存储库

 o---o---o---o---o---o---o---o---o
 N   O   P   Q   R   S   T   U   V

|         | |         | |         |
+---------+ +---------+ +---------+
 feature 1   feature 2   feature 3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将develop分支中的git提交合并到一个功能分支 - How to merge git commits in the develop branch to a feature branch 如何在不丢失提交的情况下将 GIT 合并到一个分支中? - How can I merge GIT into one branch without losing commits? Git:master/develop/feature 分支合并提交 - Git: master / develop / feature branch merge commits 如果我忘记推送提交并从本地开发分支创建功能分支,如何解决 GIT 冲突? - How to resolve GIT conflict if I forgot to push the commits and created a feature branch from my local develop branch? 在 Git 中,我如何重新设置 + 压缩一个在其历史中具有多个合并提交的分支,而无需挑选到一个全新的分支 - In Git, How can I rebase + squash a branch that has multiple merge commits in it's history without cherry-picking onto a totally new branch 如何在git的开发分支中合并本地提交? - how to merge local commits at a develop branch in git? 如何合并git分支以使主题提交成为孤立的? - How can I merge a git branch so that topic commits are orphaned? 如何在git中将文件历史记录从一个分支合并到另一个分支? - How can I merge a file history from a branch to another in git? 我如何从另一个git分支合并,除了一些提交,并且有理智/易于查询的历史记录? - How do I merge from another git branch, excepting some commits, and have sane/easily queried history? 如何在Git中“退出”功能分支合并? - How can I “back out” a feature branch merge in Git?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM