vkaul11
vkaul11

Reputation: 4214

creating local branch from remote develop branch

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 

Upvotes: 8

Views: 7511

Answers (2)

NormandErwan
NormandErwan

Reputation: 447

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.

Upvotes: 1

Gabriele Petronella
Gabriele Petronella

Reputation: 108091

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.

Upvotes: 18

Related Questions