soccerway
soccerway

Reputation: 11961

When do we need to use --track command in git

I have two branches in GitHub, master & testbranch. When ever I am creating a new branch, I type following git command to create a new branch git checkout -b testbranchnew. So in what scenario should I use git checkout --track origin/testbranchnew ? Could someone please advise ? `

Branches

  1. master

  2. testbranch

Upvotes: 1

Views: 292

Answers (2)

VonC
VonC

Reputation: 1324317

First, you don't have to use git checkout anymore. Since Git 2.23 (Aug. 2019), there is git switch

Second, you only have to use --track to set the upstream branch for a local branch, if the name of the upstream branch differs from the local one.

That assumes your local repo, in which you are creating a local branch:

  • has a remote repository (generally referenced as origin)
  • has fetched said remote repository (remote branches are fetched to your local repo)

By default, --track is used automatically: from git switch man page

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

$ git switch -c <branch> --track <remote>/<branch>

So in that case, you don't even have to use --track.

And if the remote branch does not yet exist, a git push -u origin yourBranch would automatically set the upstream relationship between the local and remote branch. See "Why do I need to explicitly push a new branch?". Again, in that case, no need for --track.

Upvotes: 3

ignoring_gravity
ignoring_gravity

Reputation: 10486

If you don't track anything, then pushing from testbranch will be

git push origin testbranch

If instead you were tracking origin/testbranch, then you could just do

git push

from testbranch and it'll be pushed to origin/testbranch.

I typically do

git push -u origin HEAD

on my first push from a new branch (so it tracks its counterpart on origin) and then git push after that.

Upvotes: 0

Related Questions