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.
This question is related to
git
git-branch
git-workflow
git-flow
To create a branch from another branch in your local directory you can use following command.
git checkout -b <sub-branch> branch
git checkout -b XYZ ABC
For creating a branch from another one can use this syntax as well:
git push origin refs/heads/<sourceBranch>:refs/heads/<targetBranch>
It is a little shorter than "git checkout -b " + "git push origin "
Create a Branch
$ git branch branch1
$ 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
Git 2.23 introduces git switch
and git restore
to split the responsibilities of git checkout
Creating a new branch from an existing branch as of git 2.23:
git switch -c my-new-branch
Switched to a new branch 'my-new-branch'
Take a look at this Github blog post explaining the changes in greater detail:
Git 2.23 brings a new pair of experimental commands to the suite of existing ones: git switch and git restore. These two are meant to eventually provide a better interface for the well-known git checkout. The new commands intend to each have a clear separation, neatly divvying up what the many responsibilities of git checkout
Switch to the develop branch:
$ git checkout develop
Creates feature/foo branch of develop.
$ git checkout -b feature/foo develop
merge the changes to develop without a fast-forward
$ git checkout develop
$ git merge --no-ff myFeature
Now push changes to the server
$ git push origin develop
$ git push origin feature/foo
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.
Create branch2
from branch1
while you have any branch whatsoever checked out (ex: let's say you have master
checked out):
git branch branch2 branch1
The general format is:
git branch <new_branch> [from_branch]
man git branch
shows it as:
git branch [--track | --no-track] [-l] [-f] <branchname> [<start-point>]
git branch new_branch
This is great for making backups before rebasing, squashing, hard resetting, etc.--before doing anything which could mess up your branch badly.
Ex: I'm on feature_branch1
, and I'm about to squash 20 commits into 1 using git rebase -i master
. In case I ever want to "undo" this, let's back up this branch first! I do this ALL...THE...TIME and find it super helpful and comforting to know I can always easily go back to this backup branch and re-branch off of it to try again in case I mess up feature_branch1
in the process:
git branch feature_branch1_BAK_20200814-1320hrs_about_to_squash
The 20200814-1120hrs
part is the date and time in format YYYYMMDD-HHMMhrs
, so that would be 13:20hrs (1:20pm) on 14 Aug. 2020. This way I have an easy way to find my backup branches until I'm sure I'm ready to delete them. If you don't do this and you mess up badly, you have to use git reflog
to go find your branch prior to messing it up, which is much harder, more stressful, and more error-prone.
git checkout -b new_branch
Just like renaming a regular file or folder in the terminal, git
considered "renaming" to be more like a 'm'ove command, so you use git branch -m
to rename a branch. Here's the general format:
git branch -m <old_name> <new_name>
man git branch
shows it like this:
git branch (-m | -M) [<oldbranch>] <newbranch>
Example: let's rename branch_1
to branch_1.5
:
git branch -m branch_1 branch_1.5
Do simultaneous work on the dev
branch. What happens is that in your scenario the feature branch moves forward from the tip of the dev branch, but the dev branch does not change. It's easier to draw as a straight line, because it can be thought of as forward motion. You made it to point A on dev, and from there you simply continued on a parallel path. The two branches have not really diverged.
Now, if you make a commit on dev, before merging, you will again begin at the same commit, A, but now features will go to C and dev to B. This will show the split you are trying to visualize, as the branches have now diverged.
*-----*Dev-------*Feature
Versus
/----*DevB
*-----*DevA
\----*FeatureC
If you want to make a branch from some another branch then follow bellow steps:
Assumptions:
BranchExisting
is the name of branch from which you need to make a new branch with name BranchMyNew
.Steps:
Fetch the branch to your local machine.
$ git fetch origin BranchExisting : BranchExisting
This command will create a new branch in your local with same branch name.
Now, from master branch checkout to the newly fetched branch
$ git checkout BranchExisting
You are now in BranchExisting. Now create a new branch from this existing branch.
$ git checkout -b BranchMyNew
Here you go!
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:
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 branch1
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).
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
Source: Stackoverflow.com