gitgit-branchgit-workflowgit-flow

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 fast-forward merged, and I don't understand why...

What am I doing wrong?

How can 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.


Solution

  • 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 the 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 the changes to the server

    git push origin dev
    git push origin myFeature
    

    And you'll see it how you want it.