我有两个分支,电子邮件和分期。分期是最新的一个,我不再需要旧的更改在电子邮件分支,但我不想删除他们。

我只想将所有staging的内容转储到电子邮件中,以便它们都指向同一个提交。这可能吗?


当前回答

其他答案给了我正确的线索,但它们并没有完全起作用。

以下是对我有效的方法:

$ git checkout email
$ git tag old-email-branch # This is optional
$ git reset --hard staging
$
$ # Using a custom commit message for the merge below
$ git merge -m 'Merge -s our where _ours_ is the branch staging' -s ours origin/email
$ git push origin email

如果没有与我们的战略合并的第四步,推送被认为是非快进更新,将被拒绝(GitHub)。

其他回答

其他答案给了我正确的线索,但它们并没有完全起作用。

以下是对我有效的方法:

$ git checkout email
$ git tag old-email-branch # This is optional
$ git reset --hard staging
$
$ # Using a custom commit message for the merge below
$ git merge -m 'Merge -s our where _ours_ is the branch staging' -s ours origin/email
$ git push origin email

如果没有与我们的战略合并的第四步,推送被认为是非快进更新,将被拒绝(GitHub)。

如果你只想让“email”和“staging”这两个分支保持一致,你可以标记“email”分支,然后将“email”分支重置为“staging”分支:

$ git checkout email
$ git tag old-email-branch
$ git reset --hard staging

你也可以将“staging”分支重新建立在“email”分支上。但是结果将包含两个分支的修改。

你想要的是这个(实际上与目前接受的答案完全相反):

git checkout email
git merge --strategy-option=theirs staging  

它的作用是:

电子邮件分支文件现在将完全相同的分期分支 电子邮件分支的历史将被保留 分期分支的历史将被添加到电子邮件历史


作为附加价值,如果您不想要所有登台分支的历史记录,您可以使用squash将其总结为一条提交消息。

git checkout email
git merge --squash --strategy-option=theirs staging  
git commit -m "Single commit message for squash branch's history here'

总之,第二个版本所做的是:

电子邮件分支文件现在将完全相同的分期分支 电子邮件分支的历史将被保留 单个提交将被添加到电子邮件分支的历史记录之上。这个提交将表示在暂存分支中发生的所有更改

我尝试了@knittl的写树/提交树方法。

分支-a:保留分支

分支-b:废弃分支

// goto branch-a branch
$ git checkout branch-a

$ git write-tree
6fa6989240d2fc6490f8215682a20c63dac5560a // echo tree id? I guess

$ git commit-tree  -p branch-a -p branch-b 6fa6989240d2fc6490f8215682a20c63dac5560a
<type some commit message end with Ctrl-d>
20bc36a2b0f2537ed11328d1aedd9c3cff2e87e9 // echo new commit id

$ git reset --hard 20bc36a2b0f2537ed11328d1aedd9c3cff2e87e9

我已经看到了几个答案,这是唯一的程序,让我解决没有任何冲突。

如果你想要在branch_old中对branch_new的所有更改,那么:

git checkout branch_new
git merge -s ours branch_old
git checkout branch_old
git merge branch_new

一旦应用了这四个命令,您就可以毫无问题地推送branch_old了