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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With