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

后端 未结 4 507
慢半拍i
慢半拍i 2021-02-02 08:33

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 t

相关标签:
4条回答
  • 2021-02-02 08:35

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

    git checkout beta
    git branch -f staging
    

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

    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.
    
    0 讨论(0)
  • 2021-02-02 08:40

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

    git branch -D staging
    git checkout beta
    git branch staging
    
    0 讨论(0)
  • 2021-02-02 08:50

    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 staging will have the work of your beta branch.

    0 讨论(0)
  • 2021-02-02 09:00

    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
    
    0 讨论(0)
提交回复
热议问题