Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating branch inside a branch in git

In my repository, I have a master branch and then a staging branch coming out of master branch. Now I need to add a third branch that should come out from staging branch. That means I need a branch coming out of another branch. Can anyone help in this?

The syntax I used for creating branch is like this:

git branch <name_of_your_new_branch>

git push origin <name_of_your_new_branch>

git checkout <name_of_your_new_branch>
like image 782
Rahul Dolas Avatar asked Nov 12 '12 12:11

Rahul Dolas


People also ask

Can I create a branch inside a branch in git?

Yes, you can create sub-branches to any branch. Lets say you have a master branch then from the master branch you can create a sub-branch. You can further create sub-branches from this child branch.

Can we create multiple branches in git?

Git offers a feature referred to as a worktree, and what it does is allow you to have multiple branches running at the same time. It does this by creating a new directory for you with a copy of your git repository that is synced between the two directories where they are stored.

How do I add a branch to a remote branch?

Push a new Git branch to a remote repoClone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch.


1 Answers

This can create your branch locally:

git checkout staging
git checkout -b newBranch

or, one line:

git checkout -b newBranch staging

That will start from the current HEAD of staging, but note that a branch doesn't really comes from another branch: it comes from a commit (and that commit can be part of multiple branches).

You can then push your new branch, tracking the the remote branch in one command:

git push -u origin newBranch    
like image 174
VonC Avatar answered Sep 21 '22 15:09

VonC