Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap a master and a feature branch

Tags:

git

branch

We find ourselves in the situation where master has new feature work, and feature1 (which branched off master) is the stable master. How can we change things so that they "trade places"? I.e., feature1 becomes master, and master becomes newfeaturebranch?

I found a couple similar SO questions, but in those cases they no longer cared about the old master commits. Here, we want to preserve the current master as the new newfeaturebranch.

I've thought of one way to do this:

  1. Create newfeaturebranch from the current master.
  2. Revert/delete commits from master back to feature1's branching point.
  3. Merge feature1 into master.
  4. Delete branch feature1.
like image 581
Dogweather Avatar asked Jun 19 '13 19:06

Dogweather


People also ask

How do I change a master to a feature branch?

Now that you made sure that your branch exists, you can switch from the master branch to the “feature” branch by executing the “git checkout” command. That's it! You have successfully switched to your “feature” branch with the checkout command.

How do I change a feature branch?

To create a new branch in Git, you use the git checkout command and pass the -b flag with a name. This will create a new branch off of the current branch. The new branch's history will start at the current place of the branch you "branched off of."

How do you override a master branch?

Using the -f flag, your previous master is completely overwritten with develop , including its history. Warning: this erases all commits from the master branch that are not also in the develop branch. This solution may be appropriate in your case if you have a small number of other branches and/or other developers.


2 Answers

You can rename branches:

git branch -m master newfeaturebranch
git branch -m feature1 master
like image 148
poke Avatar answered Oct 27 '22 04:10

poke


I started with poke's solution, but after pushing, the local branches were still tracking the old remote branches. To track the correct branches I just had to add the -u option for pushing.

git branch -m master newfeaturebranch
git branch -m feature1 master

git push -uf origin master
git push -u origin newfeaturebranch

It can be also done in two steps by first pushing and then adjusting the correct branch to track:

git push -f origin master
git push origin newfeaturebranch

git branch -u origin/master master
git branch -u origin/newfeaturebranch newfeaturebranch
like image 45
Korbinian Eckstein Avatar answered Oct 27 '22 04:10

Korbinian Eckstein