gitgithubbitbucket

How to rebase branch on top of master and discard master's conflicting commits


I have a repo with diverging master and Feature branch,like this:

master:   A -- B -- C -- etc.
                \
Feature:         \-H -- I -- J

I now realize that all the commits in the master branch after B should be discarded and Feature should become the new master. So, I would like to take all the commits in the Feature branch from H on and rebase them on top of master, discarding everything that happened in master after B. In short, I would like this:

master:   A -- B -- C -- XX      K -- etc.
                \               /
Feature:         \-H -- I -- J / 

There is an old answer (2016) that suggests this option:

git rebase -s recursive -X theirs master

The command is presumably applied (I may be wrong; there are not a lot of details in the answer) after Feature has been checked out.

So, questions:

  1. Is this old answer still applicable in the current version of git
  2. Is this just a bad idea, and should I aim for a different strategy? I thought about perhaps renaming master to old_master and moving Feature to master, but it seems recent versions of Git impose constraints on renaming the master branch, as do GitHub and BitBucket (which is where the remotes of my repo are).

BTW, this is a private repo with no other users, although it is regularly checked out from different machines.


Solution

  • You can:

    1. Save your changes on master to a new branch

    git checkout master
    git branch master_old
    git checkout master_old
    git push -u origin master_old
    

    Only if you want the old master to exist

    2. Hard reset

    git checkout master
    git reset --hard Feature
    git push --force origin master