Brian Risk
Brian Risk

Reputation: 1449

How to make existing branch a child of a new branch

A few weeks ago I branched off of our develop branch to work on a story (Agile term). Recently our release manager decided to have all branches related to a feature be children of the feature branch.

Is there an easy way to get from

develop
  |
  ---> My Branch
  |
  ---> Feature

To:

develop
  |
  ---> Feature
         |
         ---> My Branch

EDIT: I didn't realize that branches don't actually have parent/child relationships. This follows the trend of half of my SO posts making me sound straight-up ignorant. I appreciate the answers; they helped me find a good path forward.

Upvotes: 0

Views: 1287

Answers (2)

Mark Adelsberger
Mark Adelsberger

Reputation: 45749

If you need to do anything, then rebase (as others have suggested) would probably be what to do.

But there's a good chance you don't have to do anything.

People get hung up on concepts like "parent branch", that simply don't exist in git. There is no memory of what branch you were on when My Branch was created. So only if the commit topology is actually wrong does anything need to be done.

Suppose you start with

x --- O <--(develop)

And you created your story branch, and maybe did some work.

x --- O <--(develop)
       \
        A <--(story)

Now if your release manager hadn't decided to group story branches into feature branches, you'd eventually merge story back to develop. But instead you now have a feature branch, But creating the feature branch doesn't change anything about the commits that exist.

x --- O <--(develop)(feature)
       \
        A <--(story)

and in fact, even if the feature branch had existed before the story branch, and you had checked it out and used that as your starting point when you created story... the picture would be exactly the same. In this scenario, all that needs to happen is, when the story branch is done, you merge it to feature instead of to develop. (Which, assuming --no-ff, gives

x --- O <--(develop)
      |
      |-- M <--(feature)
       \ /
        A <--(story)

and you have your "apparent" branch hierarchy.)

That's not to say that you'd never have to fix anything. If there had been commits to develop between where story was created and where feature was created, you might indeed want to rebase story to make it consistent with the designated branch point for feature. That would typically look like

rebase --onto feature develop story

(And then, ideally, you should retest each and every commit on the rewritten story branch. Which, if you have automated build/test tools, shouldn't be a big deal... and if you don't, you're not really doing agile...)

And going forward: once feature has been created, you'll want to use it as the starting point for additional story branches (a) to avoid accidentally incorporating other develop commits into feature, and (b) to reduce unnecessary merge conflicts when combining storys into feature.

Upvotes: 1

merlin2011
merlin2011

Reputation: 75585

Branches do not have children, but if you want the commit at the current tip of Feature branch to be an ancestor of My Branch, you should first checkout My Branch and then rebase it on top of Feature branch.

git checkout My Branch
git rebase Feature

Note that if you want to maintain this relationship, then you must perform this rebase again every time Feature branch moves. That is, you must re-run the above two commands every time anyone makes a commit to Feature branch.

Upvotes: 0

Related Questions