【问题标题】:How can I overwrite, not merge, one remote branch into another branch?如何将一个远程分支覆盖而不是合并到另一个分支?
【发布时间】:2023-03-14 17:31:02
【问题描述】:

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

我看到有人建议这样做:

git checkout staging
git merge -s ours beta

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

【问题讨论】:

  • staging 是否在 beta 前面?你的两个分支之间究竟是什么关系?
  • 他们都有一些数据在前面。但我不想在 Staging 上做任何事情。

标签: git branching-and-merging


【解决方案1】:

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

git branch -D staging
git checkout beta
git branch staging

【讨论】:

  • 我也想过这个,但是我们没有足够的关于分支的信息。我认为如果他只需要删除他的分支,OP 就不会问这个问题
  • @Rerito:我不同意。有时人们只是看不到简单的解决方案。
  • 删除分支会破坏它的历史吗?
  • @Trip:是的。这不是你想要的吗?
  • @Amber:无处可去。我说他做了吗?不,但是我从“Staging 中包含我根本不想要的代码(包括文件)”推断出来。如果他对暂存中的文件不感兴趣,那么导致这些文件的历史有什么用?
【解决方案2】:

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

git checkout beta
git branch -f staging

如果您关心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.

【讨论】:

  • 这会删除最近在 Staging 上创建的不需要的文件?
  • @Trip 因为它会将索引重置回beta 中的确切位置,所以就 Git 而言,任何不在测试版中的东西都应该被删除。
  • 不幸的是,git merge -s theirs 在较新版本的 Git 中不可用,所以这不会完全按照所写的那样工作。
  • 如果你有更新版本的 git 使用 'git merge -X theirs' 而不是 'git merge -s theirs'。
【解决方案3】:

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

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

如果你真的无法忍受周围有那个额外的分支:

git branch -D staging_oops

【讨论】:

    【解决方案4】:

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

    git checkout staging 
    git reset --hard beta
    

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

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2010-12-15
      • 2022-08-17
      相关资源
      最近更新 更多