We have been working on a codebase with one git [master] branch.

We have got to a point now where we actually need to branch it for one feature and remove that feature from the master branch.

So we currently have

[master] <- one branch with feature 1 and feature 2

But I want to get it to look like this

[master]    <- only feature 1
[feature-2] < - feature 1 and feature 2 (what the master is now)

What is the best way to do this? The only way I can think is to create [feature-2] branch from the master, then just delete all the code I don't want in master.

However, we may have bug fixes etc for feature 1 that need to be done in [master] and then merged into [feature-2].

I can't just branch from an old commit either as the two features have been developed somewhat concurrently.

Is there a way to deal with this scenario better so I can still merge branches together etc?



If it happens that you have a previous commit where you just commited feature-1, you can just point the master branch to that commit.

According to this, you can achieve that by running:

git checkout master
git reset --hard <hash_of_the_commit>

If not, I think the only way to achieve that is doing what you said, branching and then deleting any unwanted code.

As to the second question, you will probably develop the hotfix in a different branch and then you can apply it to any other branch that you would need.

git checkout master
git merge hotfix
git checkout feature-1
git merge hotfix
git checkout feature-2
git merge hotfix

You can see more on this here, which describes exactly what you are asking.

  • Yeah I can't do the first one as they were developed together. I think maybe I need to just delete feature 2 code. Commit it, then branch and then just re add all the code for feature 2 into thast branch. The only issue there is I lose all the hostiry for the files. – noShowP Aug 27 '15 at 12:34
  • In the case you didn't commit yet both features on master, you can simply stash the changes (man git-stash) and save them into another branch. Unfortunately, if you already commited it I don't see another option than redoing the master branch with all the nuisance it entails. – nKn Aug 27 '15 at 12:49
  • Yeah I think its going to be a nusiance. Bad planning from the start! Ah well, thanks anyway. – noShowP Aug 27 '15 at 13:14

I think that the most logical way is to keep the stable code for both features in the master branch, and the development code related to feature 1 in a branch feature-1 and the code of feature 2 in another branch feature-2. When your code becomes stable in these branches you can merge them to master.

In order to do that, create the branch feature-2, then in master do a git reset --hard to the last commit before beginning feature 2, then create the branch feature-1.

  • There is no last commit is the thing. Both features were developed at the same time so the commits are happening at the same time. i.e. yesterday some people committed things for feature-1, and some people for feature-2. – noShowP Aug 27 '15 at 12:34
  • So, if you can separate the feature 1 code and feature 2 code from initial code then you can cut their codes then create a branch for each feature, checkout to feature 1 branch, past its code, then do the same for the feature 2. When the code in a branch will be ready to integrate you can merge it to master. – Hamza Abbad Aug 27 '15 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.