0

I have a git branch structure as

     feature
     /
    /
o master
    \
     \
    develop

master and develop are currently same, i.e. no additional commits in develop.

Now, I want to move the feature branch's parent to develop, so that the structure becomes:

o master
    \
     \
    develop
        \
         \
        feature

It is not necessary to preserve commit history of feature. I just want to move the current state of feature to a branch whose parent is develop. This is required so that in future it can be merged with develop without affecting master.

I am new to git and have no idea about merging / rebase etc. Detailed steps would be appreciated.

1
  • 1
    A branch does not have a 'parent' branch. Like @axiac said, there is nothing to do. Branches point to commits, not to other branches.
    – Ikke
    Commented Jul 21, 2016 at 8:08

2 Answers 2

2

master and develop are currently same, i.e. no additional commits in develop.

You don't have to do anything. Your repo already is in the status you want.

However, only the branch that is currently checked out changes when you add new commits; the others stay where they are. This means, if you checkout develop and commit something on it, your repo will reach the status described in the first diagram.

The command you need go put it in the state described in the second diagram is:

git rebase develop feature

Alternatively, you can do:

git checkout feature
git rebase develop

This is what the first form of the command does internally.

As with any Git command that manipulates the history, it requires a clean working directory to work. You have to commit (or stash) everything before you do a rebase.

1
  • Thanks for the details.
    – UditS
    Commented Jul 21, 2016 at 9:31
1

master and develop are currently same, i.e. no additional commits in develop.

Then you are done already. You do not need to do anything. Branches are not related to another, i.e., there is no concept of "parent branch" in git.

A branch is simply a name pointing to a commit, an alias if you will. A commit "is" (quite literally) the cryptic hash value you are used to work with (i.e., shown by git log). The commits themselves are actual "first class" objects, a branch is simply a pair of strings (one is the name, the other a commit hash).

Now, I want to move the feature branch's parent to develop

That's what git rebase, or in your case git rebase feature develop is for. It does exactly what you ask and is perfectly suited for that job. In your particular situation, where develop and master point to the same commit, this is a no-op, but in any other case, where develop has moved on from master, you will use rebase.

By the way, you will find a lot of advice to avoid rebase, just ignore that for now. It is an awesome tool in git, unless approached with prejudice or ignorance. Care has to be taken when working together with others (i.e., with distributed repositories, push/pull); you cannot really break anything, just make it hard for your colleagues. But that's for another question.

3
  • Thanks for the details and the Introduction links.
    – UditS
    Commented Jul 21, 2016 at 9:31
  • I have one question - When feature is merged into develop, will it effect master?
    – UditS
    Commented Jul 21, 2016 at 10:43
  • Great.. That's what I was looking for.
    – UditS
    Commented Jul 21, 2016 at 16:23

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