git:如何将一些提交移动到新分支

发布于 2024-09-07 15:39:08 字数 386 浏览 0 评论 0原文

我一直在直线工作:

A---B---C---D---E---F (master:HEAD)

现在我想向后移动:

git checkout C

并将最后几次提交移到新分支:

选项 1:

          D---E---F (new:HEAD)
         /
A---B---C (master)

选项 2:

          F (new:HEAD)
         /
A---B---C (master)

如何重新设置为选项 1 以及如何设置选项 2?

I have been working in straight line:

A---B---C---D---E---F (master:HEAD)

Now I want to move backward:

git checkout C

and move few last commits to a new branch:

Option 1:

          D---E---F (new:HEAD)
         /
A---B---C (master)

Option 2:

          F (new:HEAD)
         /
A---B---C (master)

How to rebase to Option 1 and how to Option 2?

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

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

发布评论

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

评论(1

玩套路吗 2024-09-14 15:39:08

要从第一个图表 (master = HEAD = F) 到选项 1:

git branch new        # Make a 'new' branch pointing at HEAD, which is F
git reset --hard C    # Move master back to point at C
git checkout new      # Make HEAD follow new, and get F in the working tree

从选项 1 到选项 2(从上面离开的地方继续),

git rebase -i master  # Start the process of re-jiggering this branch
# edit the commit list that opens up to only include F
# save and exit
# resolve potential conflicts from missing changes in D and E

要直接从起点转到选项 2:

git checkout -b new C  # Start the 'new' branch at C
git cherry-pick F      # Include F on it
git checkout master    # Switch back to master
git reset --hard C     # Rewind master to the earlier commit

To get from your first diagram (master = HEAD = F) to option 1:

git branch new        # Make a 'new' branch pointing at HEAD, which is F
git reset --hard C    # Move master back to point at C
git checkout new      # Make HEAD follow new, and get F in the working tree

And from option 1 to option 2 (picking up where the above left off),

git rebase -i master  # Start the process of re-jiggering this branch
# edit the commit list that opens up to only include F
# save and exit
# resolve potential conflicts from missing changes in D and E

To go directly from your starting point to option 2:

git checkout -b new C  # Start the 'new' branch at C
git cherry-pick F      # Include F on it
git checkout master    # Switch back to master
git reset --hard C     # Rewind master to the earlier commit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文