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

前端 未结 13 1282
广开言路
广开言路 2020-11-27 08:51

I have two branches, email and staging. staging is the latest one and I no longer need the old changes in email branch, y

13条回答
  •  有刺的猬
    2020-11-27 09:45

    What you want is this (actually the exact inverse of the currently accepted answer):

    git checkout email
    git merge --strategy-option=theirs staging  
    

    What this does is:

    • email branch files will now be exactly the same as staging branch
    • email branch's history will be maintained
    • staging branch's history will be added to email history

    As added value, if you don't want all of staging branch's history, you can use squash to summarize it into a single commit message.

    git checkout email
    git merge --squash --strategy-option=theirs staging  
    git commit -m "Single commit message for squash branch's history here'
    

    So in summary, what this second version does is:

    • email branch files will now be exactly the same as staging branch
    • email branch's history will be maintained
    • A single commit will be added on top of email branch's history. This commit will represent ALL the changes that took place in the staging branch

提交回复
热议问题