[英]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 一般的方法是
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:
但它们不是一个线性顺序:
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.