Skip to content

Instantly share code, notes, and snippets.

@miku
Last active July 14, 2022 17:21
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save miku/613ccf7a7030a6f32df1 to your computer and use it in GitHub Desktop.
Save miku/613ccf7a7030a6f32df1 to your computer and use it in GitHub Desktop.
git --track vs --set-upstream vs --set-upstream-to

README

Short excursion into git --track, --set-upstream and --set-upstream-to.

All examples use the aliases co for checkout and br for branch.

Setup:

$ git clone git@github.com:AKSW/OntoWiki.git
$ cd OntoWiki

[develop] $ cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = git@github.com:AKSW/OntoWiki.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "develop"]
    remote = origin
    merge = refs/heads/develop

Automatic tracking branches are the default.

[develop] $ git co -b feature/mobile origin/feature/mobile
Branch feature/mobile set up to track remote branch feature/mobile from origin.
Switched to a new branch 'feature/mobile'    

[feature/mobile] $ cat .git/config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = git@github.com:AKSW/OntoWiki.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "develop"]
    remote = origin
    merge = refs/heads/develop
[branch "feature/mobile"]
    remote = origin
    merge = refs/heads/feature/mobile

Alternatively, suppress tracking with --no-track:

[develop] $ git co --no-track -b feature/mobile origin/feature/mobile
Switched to a new branch 'feature/mobile'

[feature/mobile] $ cat .git/config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = git@github.com:AKSW/OntoWiki.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "develop"]
    remote = origin
    merge = refs/heads/develop

If you git push on a non-tracking branch, you'll get some reminder:

[feature/mobile] $ git push 
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

In Git 2.0, Git will default to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

ERROR: Permission to AKSW/OntoWiki.git denied to miku.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

You could set the upstream during git push via (deprecated) --set-upstream:

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull[1] and other commands. For more information, see branch..merge in git-config[1].

Or you have the same effect, by using git branch --set-upstream-to:

[feature/mobile] $ git br --set-upstream-to origin/feature/mobile

[feature/mobile] $ cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = git@github.com:AKSW/OntoWiki.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "develop"]
    remote = origin
    merge = refs/heads/develop
[branch "feature/mobile"]
    remote = origin
    merge = refs/heads/feature/mobile

You can unset the tracking as well:

[feature/mobile] $ git br --unset-upstream
[feature/mobile] $ cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = git@github.com:AKSW/OntoWiki.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "develop"]
    remote = origin
    merge = refs/heads/develop
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "feature/mobile"]
@felipe1982
Copy link

that helped. thank you.

@starwavelin
Copy link

Thanks for your tips!!

@RichardBronosky
Copy link

Helpful, but git --track is not mentioned except in the description.

@rafegoldberg
Copy link

rafegoldberg commented Nov 8, 2017

@RichardBronosky I believe that’s because it’s the implicit behavior? Per the author:

Automatic tracking branches are the default

Which, if I understand correctly, means the remote name will automatically be set to match the name of the local branch. (Unless you specify a remote starting point, I’d gather...)

@zeroxia
Copy link

zeroxia commented Jun 29, 2021

If in your .git/config's [remote "origin"] section, you don't have the default "fetch" line like in the example, instead, your fetch line is "branch-specific":

[remote "origin"]
    fetch = +refs/heads/some-branch:refs/remotes/origin/some-branch

And then if you want to call --set-upstrea-to for another branch, the command will fail. Error message is like:

fatal: Cannot setup tracking information; starting point 'remotes/origin/bugfix/another-branch' is not a branch.

While you are sure remotes/origin/bugfix/another-branch is a valid branch ref, this message is just very confusing (and very annoying).

Simple fix is to just change the said above "fetch" line to the default one like in original example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment