如何通过多个步骤重写已发布的 git 分支的历史记录?
我有一个包含两个分支的 git 存储库:master
和 amazing_new_feature
。后一个分支包含一项令人惊叹的新功能的工作。我和一位同事都在同一个存储库上工作,并且我们两个都致力于两个分支。
现在,这项令人惊叹的新功能的工作已经完成,并且在 amazing_new_feature
分支中积累了 100 多个提交。在将工作合并到 master 之前,我想稍微清理一下这些提交(使用 git rebase -i )。
我们面临的问题是一次性重写/重新排序所有 100 个提交是一件非常痛苦的事情。相反,我想做的是:
- 重写/合并/重新排序amazing_new_feature分支中的前几次提交,并将结果放入包含“清理”历史记录的专用分支中(例如,一个amazing_new_feature_ready_for_merge分支)。
- 将剩余的
amazing_new_feature
分支重新设置为amazing_new_feature_ready_for_merge
分支。 - 重复 1。
我的想法是,在某个时刻,amazing_new_feature 的所有工作都应该位于amazing_new_feature_ready_for_merge 中,然后我可以将后者合并到 master 中>。
这是一个明智的方法,还是有更好/更简单/更傻瓜的解决方案来解决这个问题?我特别害怕上述算法的第二步,因为它意味着重新确定已发布分支的基础。 IIRC 这是一件危险的事情。
I've got a git repository with two branches, master
and amazing_new_feature
. The latter branch contains the work on, well, an amazing new feature. A colleague and me are both working on the same repository, and the two of us commit to both branches.
Now the work on the amazing new feature finished, and a bit more than 100 commits were accumulated in the amazing_new_feature
branch. I'd like to clean those commits up a bit (using git rebase -i
) before merging the work into master.
The issue we're facing is that it's quite a pain to rewrite/reorder all 100 commits in one go. Instead, what I'd like to do is:
- Rewrite/merge/reorder the first few commits in the
amazing_new_feature
branch and put the result into a dedicated branch which contains the 'cleaned up' history (say, aamazing_new_feature_ready_for_merge
branch). - Rebase the remaining
amazing_new_feature
branch on theamazing_new_feature_ready_for_merge
branch. - Repeat at 1.
My idea is that at some point, all the work from amazing_new_feature
should be in amazing_new_feature_ready_for_merge
and then I can merge the latter into master
.
Is this a sensible approach, or are there better/easier/more fool-proff solutions to this problem? I'm especially scared about the second step of the above algorithm since it means rebasing a published branch. IIRC it's a dangerous thing to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常你不能,特别是如果该分支已广泛分布。
如果只有两个开发人员拉/推该分支,您可以同意修改它,然后另一位开发人员可以重置他/她的本地分支以引用他/她刚刚拉的远程分支(如在此所以答案)。
在这种情况下(有限分发),您在问题中描述的步骤是合理的。
Normally you cannot, especially if that branch has been widely distributed.
If you are only two developers pulling/pushing that branch, you can agree to modify it and then the other developer can reset his/her local branch to reference the remote one he/she just pulled (as in this SO answer).
In that case (limited distribution), the steps you describe in your question are sound.