git 如何将一个远程分支覆盖而不是合并到另一个分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16171813/
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 16:01:32  来源:igfitidea点击:

How can I overwrite, not merge, one remote branch into another branch?

gitbranching-and-merging

提问by Trip

I have two branches. Staging and Beta. Staging has code in it ( including files ), that I do not want at all. How can I make Beta completely overwrite Staging, so that none of those files or code are merged from Staging into Beta.

我有两个分支。分期和测试版。暂存中有代码(包括文件),我根本不想要。如何让 Beta 完全覆盖 Staging,以便这些文件或代码都不会从 Staging 合并到 Beta 中。

I see some people recommend doing this :

我看到有些人建议这样做:

git checkout staging
git merge -s ours beta

But I don't believe the pre-existing files would be a "code conflict" and therefore would not be removed. Am I wrong? If I'm right, how would I accomplish this?

但我不相信预先存在的文件会是“代码冲突”,因此不会被删除。我错了吗?如果我是对的,我将如何实现这一目标?

采纳答案by Daniel Hilgarth

You can simple delete stagingand re-create it based on beta:

您可以简单地删除staging并基于以下内容重新创建它beta

git branch -D staging
git checkout beta
git branch staging

回答by Amber

If you don't careabout the old history of staging, you can just recreate it:

如果你不关心的旧历史staging,你可以重新创建它:

git checkout beta
git branch -f staging

If you do careabout the old history of staging, then things get more fun:

如果您确实关心的旧历史staging,那么事情会变得更有趣:

git checkout staging        # First, merge beta into staging so we have
git merge -s theirs beta    # a merge commit to work with.

git checkout beta           # Then, flip back to beta's version of the files

git reset --soft staging    # Then we go back to the merge commit SHA, but keep 
                            # the actual files and index as they were in beta

git commit --amend          # Finally, update the merge commit to match the
                            # files and index as they were in beta.

回答by Shawn Balestracci

I suggest you just rename it in case you change your mind.

我建议你只是重命名它以防你改变主意。

git branch -m staging staging_oops
git checkout beta
git branch staging

If you really can't stand having that extra branch around:

如果你真的无法忍受额外的分支:

git branch -D staging_oops

回答by yardstick17

If the history of staging is not going to be an issue, you can simply do this.

如果分期历史不会成为问题,您可以简单地执行此操作。

git checkout staging 
git reset --hard beta

Just remember the history of staging will be gone after the above command and stagingwill have the work of your betabranch.

请记住,在执行上述命令后,暂存历史将消失, 暂存将拥有您的beta分支的工作。