我怎么'覆盖'而不是'合并',在Git的另一个分支上的分支?

问题描述 投票:214回答:10

我有两个分支,emailstagingstaging是最新的,我不再需要email分支的旧变化,但我不想删除它们。

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

git
10个回答
173
投票

您可以使用“我们的”合并策略:

$ git checkout staging
$ git merge -s ours email # Merge branches, but use our branch head

0
投票

最简单的方法:

//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

现在电子邮件分支和分段是相同的。


84
投票

如果您只想让两个分支“电子邮件”和“暂存”相同,您可以标记“电子邮件”分支,然后将“电子邮件”分支重置为“分段”分支:

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

您还可以在'email'分支上重新定位'staging'分支。但结果将包含两个分支的修改。


63
投票

其他答案给了我正确的线索,但他们没有完全帮助。

Here's what worked for me:

$ 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)。


57
投票

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

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

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

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


36
投票

如果你像我一样并且你不想处理合并,你可以执行上述步骤,除了使用force而不是merge,因为它会创建一个令人分心的日志文件路径:

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

注意:仅当您真的不想再次看到电子邮件中的内容时。


15
投票

我想合并两个分支,以便使用old_branch中的内容更新new_branch中的所有内容

对我来说,这就像一个魅力:

$ git checkout new_branch
$ git merge -m 'merge message' -s ours origin/old_branch
$ git checkout old_branch
$ git merge new_branch
$ git push origin old_branch

10
投票

怎么样:

git branch -D email
git checkout staging
git checkout -b email
git push origin email --force-with-lease

5
投票

其他答案看起来不完整 我在下面已经完全尝试了,它运行良好。

注意: 1.在您尝试下面之前,请复制您的存储库,以确保安全。

细节: 1.所有开发都发生在dev分支中 2. qa分支与dev的副本相同 3.时间,dev代码需要移动/覆盖到qa分支

所以我们需要从dev分支覆盖qa分支

第1部分: 使用以下命令,旧qa已更新为较新的dev:

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现在被新的dev分支代码覆盖 2.本地是干净的(远程起源/开发不受影响)


2
投票
git checkout email
git merge -m "Making email same as staging disregarding any conflicts from email in the process" -s recursive -X theirs staging
© www.soinside.com 2019 - 2024. All rights reserved.