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

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


当前回答

git checkout email
git merge -m "Making email same as staging disregarding any conflicts from email in the process" -s recursive -X theirs 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)。

我尝试了@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

这将合并新旧两个分支,并在每次合并冲突时选择新版本:

git checkout old
git merge new
git checkout --theirs .
git add .
git commit

如果你像我一样不想处理归并,你可以执行上面的步骤,除了使用强制而不是归并,因为它会创建一个分散注意力的日志记录:

git checkout email
git reset --hard staging
git push origin email --force

注意:除非你真的不想再在邮件里看到这些东西。

这个分支不会改变原来更新的分支,并让您有机会在最终提交之前进行进一步的修改。

git checkout new -b tmp
git merge -s ours old -m 'irrelevant'
git checkout old
git merge --squash tmp
git branch -D tmp
#do any other stuff you want
git add -A; git commit -m 'foo' #commit (or however you like)