我如何“覆盖”而不是“合并”Git 中另一个分支上的一个分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4624357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:47:11  来源:igfitidea点击:

How do I 'overwrite', rather than 'merge', a branch on another branch in Git?

git

提问by Rafid

I have two branches, emailand staging. stagingis the latest one and I no longer need the old changes in emailbranch, yet I don't want to delete them.

我有两个分支,emailstaging. staging是最新的,我不再需要email分支中的旧更改,但我不想删除它们。

So I just want to dump all the contents of staginginto emailso that they both point to the same commit. Is that possible?

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

采纳答案by knittl

You can use the 'ours' merge strategy:

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

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

回答by Sylvain Defresne

If you just want the two branches 'email' and 'staging' to be the same, you can tag the 'email' branch, then reset the 'email' branch to the 'staging' one:

如果您只希望 'email' 和 'staging' 两个分支相同,您可以标记 'email' 分支,然后将 'email' 分支重置为 'staging' 分支:

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

You can also rebase the 'staging' branch on the 'email' branch. But the result will contains the modification of the two branches.

您还可以在“电子邮件”分支上重新设置“暂存”分支。但结果将包含两个分支的修改。

回答by Brugolo

I've seen several answers and that's the only procedure that let me fix that without any conflicts.

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

If you want all changes from branch_new in branch_old, then:

如果您希望在 branch_old 中对 branch_new 进行所有更改,则:

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

once applied those four commands you can push the branch_old without any problem

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

回答by Shyam Habarakada

The other answers gave me the right clues, but they didn't completely help.

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

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

Without the fourth step of merging with the ours strategy, the push is considered a non-fast-forward update and will be rejected (by GitHub).

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

回答by jsarma

If you're like me and you don't want to deal with merging, you can do the above steps, except use force instead of merge, because it will create a distracting log paper trail:

如果你像我一样不想处理合并,你可以做上面的步骤,除了使用 force 而不是合并,因为它会创建一个分散注意力的日志文件痕迹:

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

Note: This is only if you REALLY never want to see the stuff in email again.

注意:只有当您真的不想再看到电子邮件中的内容时才会这样做。

回答by Madhu

I wanted to merge two branches so that all the contents in old_branchto be updated with the contents from new_branch

我想合并两个分支,以便将所有内容old_branch更新为来自new_branch

For me this worked like a charm:

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

$ 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

回答by Arek S

How about:

怎么样:

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

回答by Manohar Reddy Poreddy

Other answers looked incomplete.
I have tried below in full, and it worked fine.

其他答案看起来不完整。
我已经在下面进行了完整的尝试,效果很好。

NOTE:
1. Make a copy of your repository before you try below, to be on safe side.

注意:
1. 为安全起见,在尝试以下操作之前,请复制您的存储库。

Details:
1. All development happens in dev branch
2. qa branch is just the same copy of dev
3. Time to time, dev code needs to be moved/overwrite to qa branch

详细信息:
1. 所有开发都发生在 dev 分支
2. qa 分支只是 dev 的同一个副本
3. 有时,需要将 dev 代码移动/覆盖到 qa 分支

so we need to overwrite qa branch, from dev branch

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

Part 1:
With below commands, old qa has been updated to newer dev:

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

git checkout dev
git merge -s ours qa
git checkout qa
git merge dev
git push

Automatic comment for last push gives below:

上次推送的自动评论如下:

// Output:
//  *<MYNAME> Merge branch 'qa' into dev,*  

This comment looks reverse, because above sequence also looks reverse

这个评论看起来是反向的,因为上面的顺序也看起来是反向的

Part 2:

第2部分:

Below are unexpected, new local commits in dev, the unnecessary ones
so, we need to throw away, and make dev untouched.

以下是 dev 中意想不到的新本地提交,这些是不必要的,
因此,我们需要扔掉,并使 dev 保持不变。

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

Part 3:
Verify everything is as expected:

第 3 部分:
验证一切是否符合预期:

git status  

// Output:
//  *On branch dev  
//  Your branch is up-to-date with 'origin/dev'.  
//  nothing to commit, working tree clean*  

That's all.
1. old qa is now overwritten by new dev branch code
2. local is clean (remote origin/dev is untouched)

就这样。
1. 旧的 qa 现在被新的开发分支代码覆盖
2. 本地是干净的(远程源/开发未受影响)

回答by B.Neo

The easiest way to do it:

最简单的方法:

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

Now the email branch and the staging are the same.

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

回答by Willa

git checkout email
git merge -m "Making email same as staging disregarding any conflicts from email in the process" -s recursive -X theirs staging