Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating local branch from remote develop branch

Tags:

git

I want to create a local and a remote branch named test from the develop branch on origin. However, even though my current local branch is tracking origin/develop when I checkout the new branch it takes origin/master. Therefore, I have to follow the steps below to get a test branch on both remote and local.

git checkout -b test ( By default it picks origin/master though my current branch tracks origin/develop)
git fetch origin
git reset --hard origin/develop 
git push -u origin test 
like image 759
vkaul11 Avatar asked Jun 21 '13 23:06

vkaul11


2 Answers

According to the documentation

git checkout -b test --track origin/develop

should do the trick.


As extra goodies, if you want to create a local branch to track a remote branch with the same name, you can be lazy an omit the -b option

git checkout --track origin/develop

will create and checkout a local branch named develop, thus being equivalent to

git checkout -b develop --track origin/develop

From the doc

As a convenience, --track without -b implies branch creation.

[...]

If no -b option is given, the name of the new branch will be derived from the remote-tracking branch.

like image 51
Gabriele Petronella Avatar answered Oct 02 '22 22:10

Gabriele Petronella


Starting with Git 2.23, you can also use:

git switch -t origin/<branch>

It creates and checkouts to a new local branch named <branch> tracking the remote origin/<branch>.

More details on the documentation.

like image 38
NormandErwan Avatar answered Oct 02 '22 21:10

NormandErwan