如何将某些提交基于git中的另一个分支? [英] How to move certain commits to be based on another branch in git?

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

问题描述

情况:


  • master在X

  • quickfix1在X + 2次提交

这样:

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)



<然后我开始使用quickfix2,但是偶然地将quickfix1作为要复制的源分支,而不是主副本。现在quickfix2是X + 2次提交+ 2次相关的提交。

Then I started working on quickfix2, but by accident took quickfix1 as the source branch to copy, not the master. Now quickfix2 is at X + 2 commits + 2 relevant commits.

o-o-X (master HEAD)
     \
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

现在,我想拥有一个带有quickfix2的分支,但不包含属于quickfix1的2个提交。

Now I want to have a branch with quickfix2, but without the 2 commits that belong to quickfix1.

      q2a'--q2b' (quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

我尝试从quickfix2中的某个修订版本创建补丁,但该补丁不保留提交历史记录。有没有办法保存我的提交历史记录,但是有一个分支在quickfix1中没有更改?

I tried to create a patch from a certain revision in quickfix2, but the patch doesn't preserve the commit history. Is there a way to save my commit history, but have a branch without changes in quickfix1?

推荐答案

rebase --onto

 # let's go to current master (X, where quickfix2 should begin)
 git checkout master

 # replay every commit *after* quickfix1 up to quickfix2 HEAD.
 git rebase --onto master quickfix1 quickfix2 

所以您应该从

o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

至:

      q2a'--q2b' (new quickfix2 HEAD)
     /
o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)

最好在干净的工作树上进行此操作。

请参阅 git config --global rebase.autostash true ,尤其是 Git 2.10之后

This is best done on a clean working tree.
See git config --global rebase.autostash true, especially after Git 2.10.

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

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