简体   繁体   English

如何将分支中的提交移动到GIT中的另一个新分支?

[英]How to move commits in a branch to another new branch in GIT?

I have develop git branch for my work, I created a new branch called feature1 and made commit 1,2,3, and 4. 我为我的工作开发了 git branch,我创建了一个名为feature1的新分支,并提交了1,2,3和4。

I need to move commit 3,4 from feature1 branch to a new created branch feature2 . 我需要将commit 3,4从feature1分支移动到新创建的分支feature2

The commits 3,4 should be deleted from feature1 and added to a new branch feature2 , so the end result should be something like feature1 with 1, and 2 branches and feature2 with 3 and 4. 应该从feature1中删除提交3,4并将其添加到新的分支feature2 ,因此最终结果应该是feature1 with 1和2 branch以及feature2 with 3和4。

Please note that at the moment I have develop and feature1 branches. 请注意,目前我已开发设有1个分支机构。 feature2 not added yet. feature2尚未添加。

What is the best way to achieve that? 实现这一目标的最佳方法是什么? I tried git cherry-pick but wanna make sure the best way to do that. 我试过git cherry-pick但是想确保最好的方法。

If I understand your description correctly, your repo currently looks like this, 如果我理解你的描述正确,你的回购目前看起来像这样,

... -- o [develop]
        \ 
         1 -- 2 -- 3 -- 4 [feature1]

and you want it to look like that 而你希望它看起来像那样

         3'-- 4'[feature2]
        /
... -- o [develop]
        \
         1 -- 2 [feature1]

Correct? 正确? If so, do the following. 如果是这样,请执行以下操作。

First, make sure you're in a clean working state. 首先,确保您处于干净的工作状态。 Then, create and check out a branch called feature2 that points at the same commit as develop : 然后,创建并签出一个名为feature2的分支,该分支指向与develop相同的提交:

git checkout -b feature2 develop

Your repo will look as follows. 您的回购将如下所示。

... -- o [HEAD=feature2,develop]
        \ 
         1 -- 2 -- 3 -- 4 [feature1]

Cherry-pick the two commits of interest ( 3 and 4 ): 樱桃选择两个感兴趣的提交( 34 ):

git cherry-pick <commit-ID-of-3> <commit-ID-of-4>

After that, Your repo will look as follows. 之后,您的仓库将如下所示。

         3'-- 4'[HEAD=feature2]
        /
... -- o [develop]
        \ 
         1 -- 2 -- 3 -- 4 [feature1]

Finally, check out your feature1 branch and reset it two commits back: 最后,检查你的feature1分支并将其重置为两次提交:

git checkout feature1
git reset --hard HEAD~2

Your repo will end up as desired, 您的回购将按预期结束,

         3'-- 4'[feature2]
        /
... -- o [develop]
        \
         1 -- 2 [HEAD=feature1]

and you'll be in a clean working state. 你将处于一个干净的工作状态。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM