git:如何将一些提交移动到新分支 [英] git: how to move some commits to new branch

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

问题描述

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

现在我想向后移动:

  git checkout C 

并且将最后几次提交移动到一个新分支:

选项1:

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

选项2:

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

如何将选项1和选项2重新组合?

解决方案

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

  git branch new#将一个'new'分支指向HEAD,即F 
git reset --hard C#将master移回C
git checkout new#使HEAD跟随新行,并在工作树中获得F

从选项1到选项2(选择上面的停止位置),

  git rebase -i master#开始重新调整这个分支的过程
#编辑打开的提交列表,只包含F
#保存并退出
#解决D和E中缺少变更的潜在冲突b


$ b

直接从你的出发点到选项2:

  git checkout -b new C#在C 
处启动'new'分支git cherry-pick F#在其上包含F
git checkout master#切换回主
git reset --hard C#将主卷回到之前的提交


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?

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆