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

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

本文介绍了如何将分支中的提交移动到GIT中的另一个新分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的工作开发了git分支,我创建了一个名为 feature1 的新分支,并提交了1,2,3和4版本。



我需要将提交3,4从 feature1 分支移至新创建的分支 feature2



提交3,4应该从 feature1 中删除并添加到一个新分支 feature2 中,以便结束结果应该像 feature1 1和2分支, feature2 3和4 。目前我有 develop feature1 分支。 feature2 尚未添加。



达到这个目标的最佳方法是什么?我尝试了git樱桃选择,但要确保最好的方式来做到这一点。

解决方案

如果我正确理解您的描述,您的回购目前看起来像这样,

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

并且您希望它看起来像那样

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

正确吗?如果是这样,请执行以下操作。



首先,确保您处于干净的工作状态。然后,创建并签出名为 feature2 的分支,该分支指向与 develop 相同的提交:

  git checkout -b功能2开发

您的回购看起来如下。

  ...  -  o [HEAD = feature2,develop] 

1 - 2 - 3 - 4 [feature1]

Cherry挑选两个感兴趣的提交( 3 4 ):

  git cherry-pick< commit-ID-of-3> <提交-ID-的-4> 

之后,您的回购将如下所示。

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

最后, code> feature1 分支并将它重置为两次提交:

  git checkout feature1 
git reset --hard HEAD〜2

您的回购将根据需要结束,

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

处于干净的工作状态。

I have develop git branch for my work, I created a new branch called feature1 and made commit 1,2,3, and 4.

I need to move commit 3,4 from feature1 branch to a new created branch 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.

Please note that at the moment I have develop and feature1 branches. feature2 not added yet.

What is the best way to achieve that? I tried git cherry-pick but wanna make sure the best way to do that.

解决方案

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:

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):

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:

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.

这篇关于如何将分支中的提交移动到GIT中的另一个新分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:24