提交的重新排序 [英] Reordering of commits

查看:110
本文介绍了提交的重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  abcdefg(branchA)
/
--oxxxxxxxxxx(主)
\
xxxxx(branchB)

(字母表示提交,而x是不相关的提交。)然而,我注意到将一些内容合并提交。我想连接提交a,d,e和g到一个补丁中并将其提交给主控。提交b和f应该作为一个提交给branchB。有没有一种很好的'git'-ish方式来实现它? 您正在寻找的命令是 git rebase ,特别是 -i / - interactive 选项。

我假设你想在分支A上留下提交c,并且确实意味着你想将其他提交移动到其他分支,而不是合并,因为合并很简单。让我们从操纵分支A开始。

  git rebase -i<提交a的SHA1> ^ branchA 

^ 表示前一次提交,所以此命令表示rebase分支使用a之前的提交作为基础。 Git会向您显示这个范围内的提交列表。对它们进行重新排序并告诉git压缩合适的:

 选择c ... 
选择一个...
squash d ...
squash e ...
squash g ...
pick b
squash f

现在历史应该如下所示:

  c -  [a + d + e + g] ​​ -  [b + f](branchA)
/
--oxxxxxxxxxx(master)

现在,让我们为branchB获取新压扁的提交b + f。

  git checkout branchB 
git cherry-pick branchA#cherry-pick one commit,branchA

同样为a + d + e + g为master:

  git checkout master 
git樱桃选择分支a ^

最后,更新branchA,使其指向c:

  git branch -f branchA branchA ^^ 

现在我们应该有:

  c(分支A) -  [a + d + e + g] ​​ - [b + f](dan gling commitits)
/
--oxxxxxxxxxx- [a + d + e + g](master)
\
xxxxx- [b + f](branchB)

请注意,如果您有多次提交需要在分支之间移动,则可以再次使用rebase(非交互式):

 #创建一个临时分支
git分支fromAtoB branchA
#移动branchA返回两个提交
git branch -f branchA branchA〜2
#rebase这两个提交到branchB
git rebase --onto branchB branchA fromAtoB
#合并(快进)这些到branchB
git checkout branchB
git合并fromAtoB
#清理
git分支-d fromAtoB

最后,一个免责声明:很有可能以这样一种方式对提交进行重新排序,以致一些不再适用于干净。这可能是因为你选择了一个错误的命令(在提交补丁前提交一个补丁)。在这种情况下,你会想要中止rebase( git rebase --abort )。否则,你将不得不智能修复冲突(就像你对合并冲突一样),添加修正,然后运行 git rebase --continue 继续前进。这些指令也由冲突发生时打印的错误消息提供。


I'm currently working on a branch and want some commits to merge into other branches:

    a-b-c-d-e-f-g (branchA)
   /
--o-x-x-x-x-x-x-x-x-x-x (master)
   \
    x-x-x-x-x (branchB)

(Letters denote commits, and the "x" are irrelevant commits.)

However I noticed that it would be a good idea to pool some commits. I want to "concatenate" commit a, d, e and g into one patch and commit it to master. Commits b and f should go as one commit to branchB. Is there a good 'git'-ish way to achieve it?

解决方案

The command you're looking for is git rebase, specifically the -i/--interactive option.

I'm going to assume you want to leave commit c on branch A, and that you really do mean you want to move the other commits to the other branches, rather than merging, since merges are straightforward. Let's start by manipulating branch A.

git rebase -i <SHA1 of commit a>^ branchA

The ^ means the previous commit, so this command says to rebase branch A using the commit before "a" as the base. Git will present you with a list of the commits in this range. Reorder them and tell git to squash the appropriate ones:

pick c ...
pick a ...
squash d ...
squash e ...
squash g ...
pick b
squash f

Now the history should look like this:

    c - [a+d+e+g] - [b+f] (branchA)
   /
--o-x-x-x-x-x-x-x-x-x-x (master)

Now, let's grab the newly-squashed commit b+f for branchB.

git checkout branchB
git cherry-pick branchA  # cherry-pick one commit, the tip of branchA

And the same for a+d+e+g for master:

git checkout master
git cherry-pick branchA^

Finally, update branchA so it points to c:

git branch -f branchA branchA^^

We should now have:

    c (branch A) - [a+d+e+g] - [b+f] (dangling commits)
   /
--o-x-x-x-x-x-x-x-x-x-x-[a+d+e+g] (master)
   \
    x-x-x-x-x-[b+f] (branchB)

Note that if you had multiple commits you wanted to move between branches, you could use rebase again (non-interactively):

# create a temporary branch
git branch fromAtoB branchA
# move branchA back two commits
git branch -f branchA branchA~2
# rebase those two commits onto branchB
git rebase --onto branchB branchA fromAtoB
# merge (fast-forward) these into branchB
git checkout branchB
git merge fromAtoB
# clean up
git branch -d fromAtoB

Finally, a disclaimer: It's quite possible to reorder commits in such a way that some no longer apply cleanly. This could be because you chose a bad order (putting a patch before the commit introducing the feature it patched); in that case you'll want to abort the rebase (git rebase --abort). Otherwise, you'll have to intelligently fix the conflicts (just as you do with merge conflicts), add the fixes, then run git rebase --continue to move on. These instructions are also provided by the error message printed when the conflict occurs.

这篇关于提交的重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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