Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a branch in Git from another branch

I have two branches: master and dev

I want to create a "feature branch" from the dev branch.

Currently on the branch dev, I do:

$ git checkout -b myfeature dev 

... (some work)

$ git commit -am "blablabla" $ git push origin myfeature 

But, after visualizing my branches, I got:

--**master** ------0-----0-----0-----0-----0 ------------------------**dev**----**myfeature** 

I mean that the branch seems ff merged, and I don't understand why...

What I'm doing wrong?

Can you explain me please how you branch off from another branch and push back to the remote repository for the feature branch?

All that in a branching model like the one described here.

like image 411
revohsalf Avatar asked Dec 17 '10 12:12

revohsalf


People also ask

Can I create a branch inside another branch?

If you want create a new branch from any of the existing branches in Git, just follow the options. First change/checkout into the branch from where you want to create a new branch. For example, if you have the following branches like: master.

How do I create a branch from two branches?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch.


2 Answers

If you like the method in the link you've posted, have a look at Git Flow.

It's a set of scripts he created for that workflow.

But to answer your question:

$ git checkout -b myFeature dev 

Creates MyFeature branch off dev. Do your work and then

$ git commit -am "Your message" 

Now merge your changes to dev without a fast-forward

$ git checkout dev $ git merge --no-ff myFeature 

Now push changes to the server

$ git push origin dev $ git push origin myFeature 

And you'll see it how you want it.

like image 81
Abizern Avatar answered Sep 22 '22 05:09

Abizern


If you want create a new branch from any of the existing branches in Git, just follow the options.

First change/checkout into the branch from where you want to create a new branch. For example, if you have the following branches like:

  • master
  • dev
  • branch1

So if you want to create a new branch called "subbranch_of_b1" under the branch named "branch1" follow the steps:

  1. Checkout or change into "branch1"

    git checkout branch1 
  2. Now create your new branch called "subbranch_of_b1" under the "branch1" using the following command.

    git checkout -b subbranch_of_b1 branch1 

    The above will create a new branch called subbranch_of_b1 under the branch branch1 (note that branch1 in the above command isn't mandatory since the HEAD is currently pointing to it, you can precise it if you are on a different branch though).

  3. Now after working with the subbranch_of_b1 you can commit and push or merge it locally or remotely.

A sample Graphical Illustration Of Creating Branches Under another Branch

push the subbranch_of_b1 to remote

 git push origin subbranch_of_b1  
like image 44
Praveen George Avatar answered Sep 20 '22 05:09

Praveen George