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

Tri*_*rip 30 git branching-and-merging

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

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

git checkout staging
git merge -s ours beta
Run Code Online (Sandbox Code Playgroud)

但我不相信预先存在的文件会是"代码冲突",因此不会被删除.我错了吗?如果我是对的,我将如何做到这一点?

Dan*_*rth 29

您可以根据以下内容简单地删除staging并重新创建它beta:

git branch -D staging
git checkout beta
git branch staging
Run Code Online (Sandbox Code Playgroud)

  • 我也想过这个,但是我们没有足够的关于分支机构的信息。我认为如果他只需要移除他的分支,OP 就不会问这个问题 (2认同)
  • @Amber:无处可去。我说他做了吗?不。但我是从“Staging 中有代码(包括文件),我根本不想要”中推断出来的。如果他对暂存的文件不感兴趣,那么导致这些文件的历史有什么用? (2认同)

Amb*_*ber 23

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

git checkout beta
git branch -f staging
Run Code Online (Sandbox Code Playgroud)

如果你关心旧的历史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.
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是`git merge -s theirs`在较新版本的Git中不可用,所以这不会完全按照书面形式工作. (2认同)
  • 如果您有更新版本的git,请使用'git merge -X theirs'而不是'git merge -s theirs'. (2认同)

Sha*_*cci 8

我建议你重新命名,以防你改变主意.

git branch -m staging staging_oops
git checkout beta
git branch staging
Run Code Online (Sandbox Code Playgroud)

如果你真的不能忍受额外的分支:

git branch -D staging_oops
Run Code Online (Sandbox Code Playgroud)