UditS
UditS

Reputation: 1956

Move one git branch to another with different Parent?

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.

Upvotes: 0

Views: 579

Answers (2)

AnoE
AnoE

Reputation: 8345

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.

Upvotes: 1

axiac
axiac

Reputation: 72226

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.

Upvotes: 2

Related Questions