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

前端 未结 13 1281
广开言路
广开言路 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:26

    I've seen several answers and that's the only procedure that let me fix that without any conflicts.

    If you want all changes from branch_new in branch_old, then:

    git checkout branch_new
    git merge -s ours branch_old
    git checkout branch_old
    git merge branch_new
    

    once applied those four commands you can push the branch_old without any problem

    0 讨论(0)
  • 2020-11-27 09:26

    How about:

    git branch -D email
    git checkout staging
    git checkout -b email
    git push origin email --force-with-lease
    
    0 讨论(0)
  • If you're like me and you don't want to deal with merging, you can do the above steps, except use force instead of merge, because it will create a distracting log paper trail:

    git checkout email
    git reset --hard staging
    git push origin email --force
    

    Note: This is only if you REALLY never want to see the stuff in email again.

    0 讨论(0)
  • 2020-11-27 09:37

    If you just want the two branches 'email' and 'staging' to be the same, you can tag the 'email' branch, then reset the 'email' branch to the 'staging' one:

    $ git checkout email
    $ git tag old-email-branch
    $ git reset --hard staging
    

    You can also rebase the 'staging' branch on the 'email' branch. But the result will contains the modification of the two branches.

    0 讨论(0)
  • 2020-11-27 09:39

    I tried @knittl's write-tree/commit-tree approach.

    branch-a: the kept branch

    branch-b: the abandoned branch

    // goto branch-a branch
    $ git checkout branch-a
    
    $ git write-tree
    6fa6989240d2fc6490f8215682a20c63dac5560a // echo tree id? I guess
    
    $ git commit-tree  -p branch-a -p branch-b 6fa6989240d2fc6490f8215682a20c63dac5560a
    <type some commit message end with Ctrl-d>
    20bc36a2b0f2537ed11328d1aedd9c3cff2e87e9 // echo new commit id
    
    $ git reset --hard 20bc36a2b0f2537ed11328d1aedd9c3cff2e87e9
    
    0 讨论(0)
  • 2020-11-27 09:40

    The easiest way to do it:

    //the branch you want to overwrite
    git checkout email 
    
    //reset to the new branch
    git reset --hard origin/staging
    
    // push to remote
    git push -f
    

    Now the email branch and the staging are the same.

    0 讨论(0)
提交回复
热议问题