What’s the difference between git “–track” and “–set-upstream-to”?

$ git checkout foo -b
$ git branch --track origin/retarget

The first two commands instruct git to:

  • create a local branch named “origin/retarget” (very bad idea, as it is named as a remote tracking branch”, while it is actually a simple local branch with a ‘/‘ in its name)
  • starting from the current branch (“foo“, another local branch)
  • to make that new local branch tracking its starting point.

See git branch

--track

When creating a new branch, set up branch.<name>.remote and branch.<name>.merge configuration entries to mark the start-point branch as “upstream” from the new branch.

You would use track when you create a branch starting from a remote tracking one.

In other word, your first example isn’t how you would use --track.
This would work better:

git checkout -b foo --track origin/retarget

Or, using the more recent (Git 2.23+, Q3 2019) git switch command:

git switch -c foo --track origin/retarget

(there is no --set-upstream-to with git switch)


As for the difference between --track and --set-upstream-to:

--set-upstream-to

If specified branch does not exist yet or if --force has been given, acts exactly like --track.
Otherwise, sets up configuration like --track would when creating the branch, except that where branch points to is not changed.


Note that with git 2.37 (Q2 2022), you can even forget about --track or --set-upstream-to with:

git config --global push.autoSetupRemote true

Then a git push origin/retarget would automatically link the current local and remote branch.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)