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...
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.
11 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.
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:
Checkout or change into "branch1"
git checkout branch1Now create your new branch called "subbranch_of_b1" under the "branch1" using the following command.
git checkout -b subbranch_of_b1 branch1The above will create a new branch called subbranch_of_b1 under the branch branch1 (note that
branch1in 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).Now after working with the subbranch_of_b1 you can commit and push or merge it locally or remotely.
push the subbranch_of_b1 to remote
git push origin subbranch_of_b1
Create a Branch
- Create branch when master branch is checked out. Here commits in master will be synced to the branch you created.
$ git branch branch1 - Create branch when branch1 is checked out . Here commits in branch1 will be synced to branch2
$ git branch branch2
Checkout a Branch
git checkout command switch branches or restore working tree files
$ git checkout branchname
Renaming a Branch
$ git branch -m branch1 newbranchname
Delete a Branch
$ git branch -d branch-to-delete$ git branch -D branch-to-delete( force deletion without checking the merged status )
Create and Switch Branch
$ git checkout -b branchname
Branches that are completely included
$ git branch --merged
************************** Branch Differences [ git diff branch1..branch2 ] ************************
Multiline difference$ git diff master..branch1
$ git diff --color-words branch1..branch2
Must Read
Various ways to create a branch in git from another branch:
This answer adds some additional insight, not already present in the existing answers, regarding just the title of the question itself (Create a branch in Git from another branch), but does not address the more narrow specifics of the question which already have sufficient answers here.
I'm adding this because I really needed to know how to do #1 below just now (create a new branch from a branch I do NOT have checked out), and it wasn't obvious how to do it, and Google searches led to here as a top search result. So, I'll share my findings here. This isn't touched upon well, if at all, by any other answer here.
While I'm at it, I'll also add my other most-common git branch commands I use in my regular workflow, below.