如何通过多个步骤重写已发布的 git 分支的历史记录?

发布于 2024-08-29 01:49:33 字数 785 浏览 6 评论 0原文

我有一个包含两个分支的 git 存储库:masteramazing_new_feature。后一个分支包含一项令人惊叹的新功能的工作。我和一位同事都在同一个存储库上工作,并且我们两个都致力于两个分支。

现在,这项令人惊叹的新功能的工作已经完成,并且在 amazing_new_feature 分支中积累了 100 多个提交。在将工作合并到 master 之前,我想稍微清理一下这些提交(使用 git rebase -i )。

我们面临的问题是一次性重写/重新排序所有 100 个提交是一件非常痛苦的事情。相反,我想做的是:

  1. 重写/合并/重新排序amazing_new_feature分支中的前几次提交,并将结果放入包含“清理”历史记录的专用分支中(例如,一个amazing_new_feature_ready_for_merge分支)。
  2. 将剩余的 amazing_new_feature 分支重新设置为 amazing_new_feature_ready_for_merge 分支。
  3. 重复 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:

  1. 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, a amazing_new_feature_ready_for_merge branch).
  2. Rebase the remaining amazing_new_feature branch on the amazing_new_feature_ready_for_merge branch.
  3. 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 技术交流群。

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

发布评论

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

评论(1

俏︾媚 2024-09-05 01:49:33

如何重写已发布的 git 分支的历史记录?

通常你不能,特别是如果该分支已广泛分布。
如果只有两个开发人员拉/推该分支,您可以同意修改它,然后另一位开发人员可以重置他/她的本地分支以引用他/她刚刚拉的远程分支(如在此所以答案)。

在这种情况下(有限分发),您在问题中描述的步骤是合理的。

How can I rewrite the history of a published git branch ?

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.

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