Skip to content

Instantly share code, notes, and snippets.

@msankhala
Forked from miku/README.md
Created August 15, 2017 00:10
Show Gist options
  • Save msankhala/0b86d0796f3de64f0ce82d834b77aba4 to your computer and use it in GitHub Desktop.
Save msankhala/0b86d0796f3de64f0ce82d834b77aba4 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"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment