简体   繁体   English

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

[英]git: how to move some commits to new branch

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: 选项1:

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

Option 2: 选项2:

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

How to rebase to Option 1 and how to Option 2? 如何重新定义选项1以及如何选择2?

To get from your first diagram (master = HEAD = F) to option 1: 要从第一个图表(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

And from option 1 to option 2 (picking up where the above left off), 从选项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

To go directly from your starting point to option 2: 直接从您的起点转到选项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

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

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