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

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

    Other answers looked incomplete.
    I have tried below in full, and it worked fine.

    NOTE:
    1. Make a copy of your repository before you try below, to be on safe side.

    Details:
    1. All development happens in dev branch
    2. qa branch is just the same copy of dev
    3. Time to time, dev code needs to be moved/overwrite to qa branch

    so we need to overwrite qa branch, from dev branch

    Part 1:
    With below commands, old qa has been updated to newer dev:

    git checkout dev
    git merge -s ours qa
    git checkout qa
    git merge dev
    git push
    

    Automatic comment for last push gives below:

    // Output:
    //  *<MYNAME> Merge branch 'qa' into dev,*  
    

    This comment looks reverse, because above sequence also looks reverse

    Part 2:

    Below are unexpected, new local commits in dev, the unnecessary ones
    so, we need to throw away, and make dev untouched.

    git checkout dev
    
    // Output:
    //  Switched to branch 'dev'  
    //  Your branch is ahead of 'origin/dev' by 15 commits.  
    //  (use "git push" to publish your local commits)
    
    
    git reset --hard origin/dev  
    
    //  Now we threw away the unexpected commits
    

    Part 3:
    Verify everything is as expected:

    git status  
    
    // Output:
    //  *On branch dev  
    //  Your branch is up-to-date with 'origin/dev'.  
    //  nothing to commit, working tree clean*  
    

    That's all.
    1. old qa is now overwritten by new dev branch code
    2. local is clean (remote origin/dev is untouched)

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