我有两个分支,电子邮件和分期。分期是最新的一个,我不再需要旧的更改在电子邮件分支,但我不想删除他们。
我只想将所有staging的内容转储到电子邮件中,以便它们都指向同一个提交。这可能吗?
我有两个分支,电子邮件和分期。分期是最新的一个,我不再需要旧的更改在电子邮件分支,但我不想删除他们。
我只想将所有staging的内容转储到电子邮件中,以便它们都指向同一个提交。这可能吗?
当前回答
这将合并新旧两个分支,并在每次合并冲突时选择新版本:
git checkout old
git merge new
git checkout --theirs .
git add .
git commit
其他回答
你想要的是这个(实际上与目前接受的答案完全相反):
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'
总之,第二个版本所做的是:
电子邮件分支文件现在将完全相同的分期分支 电子邮件分支的历史将被保留 单个提交将被添加到电子邮件分支的历史记录之上。这个提交将表示在暂存分支中发生的所有更改
其他答案似乎不完整。 我已经尝试了下面的全部,它工作得很好。
注意: 1. 为了安全起见,在尝试下面的操作之前,请先复制您的存储库。
细节: 1. 所有的开发都在开发分支中进行 2. Qa部门只是开发部门的翻版 3.有时,开发代码需要被移动/覆盖到qa部门
所以我们需要覆盖qa分支,从开发分支
第1部分: 使用以下命令,旧的qa已经更新到新的开发:
git checkout dev
git merge -s ours qa
git checkout qa
git merge dev
git push
最后一次推送的自动评论如下:
// Output:
// *<MYNAME> Merge branch 'qa' into dev,*
这个注释看起来是相反的,因为上面的序列看起来也是相反的
第2部分:
下面是意想不到的,新的本地提交在开发中,不必要的 所以,我们需要抛弃这些内容,保持开发者的原封不动。
git checkout dev
// Output:
// Switched to branch 'dev'
// Your branch is ahead of 'origin/dev' by 15 commits.
// (use "git push" to publish your local commits)
git reset --hard origin/dev
// Now we threw away the unexpected commits
第3部分: 验证一切如预期:
git status
// Output:
// *On branch dev
// Your branch is up-to-date with 'origin/dev'.
// nothing to commit, working tree clean*
这是所有。 1. 旧的qa现在被新的开发分支代码覆盖 2. 本地是干净的(远程源/dev是不受影响的)
如何:
git branch -D email
git checkout staging
git checkout -b email
git push origin email --force-with-lease
其他答案给了我正确的线索,但它们并没有完全起作用。
以下是对我有效的方法:
$ 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)。
警告:这将删除电子邮件分支上的所有提交。这就像删除电子邮件分支,并在登台分支的头部重新创建它。
最简单的方法是:
//the branch you want to overwrite
git checkout email
//reset to the new branch
git reset --hard origin/staging
// push to remote
git push -f
现在电子邮件分支和分期是相同的。