5

I am on Git version 2.6.3, and get this message when just running

git pull

"There is no tracking information for the current branch."

I was under the impression that git would default to origin and the branch with the same name under the "simple" config.

After some trouble, I discover that the easiest way to configure this is to use the -u option like so:

$ git push -u origin master

then it will say:

"Branch master set up to track remote branch master from origin."

so my question is, why can't we use the -u option with git pull?

$ git pull -u origin master

the -u option is not recognized on pull, only with push

my question is - is there a good reason for that?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 2
    Duplicate of http://stackoverflow.com/questions/32056324/ – starlocke Dec 09 '15 at 19:53
  • thanks, that question is basically the same, but I was hoping for a better answer than the one given there – Alexander Mills Dec 09 '15 at 20:01
  • @starlocke I updated my question. It's better to use the -u option, but the -u option only works with git push not git pull, and I am wondering why. Also, it's probably important to upgrade Git to version > 2.0 – Alexander Mills Dec 09 '15 at 20:29

3 Answers3

5

You can use this command to set the upstream of your current branch $ git branch --set-upstream-to=origin/master

This way your setting the upstream branch to master by default when pulling and pushing without actually using a push or pull command.

Now try to git pull and it should start gathering everything from your repository and after that it will say it's Already up-to-date

If you have any further questions, I'll be happy to assist.

ALXvirtual
  • 61
  • 6
4

-uis just a shortcut for using --set-upstream. This flag will cause your local branch to track your remote branch from remote upstream. You only need to do this action once and ideally at the begining by using git push -u origin <branch_name>.

This means that when you use git pull, git fetch and git push it should assume that your local branch and the remote branching that is tracking will sync.

If you want to know read more go here: An Asymmetry Between Git Pull and Git Push

German
  • 496
  • 1
  • 4
  • 10
3

the -u option is not recognized on pull, only with push

my question is - is there a good reason for that?

Well... actually Git 2.24 (Q4 2019, 4 years later) will provide -u for git pull/git fetch!

Official reason:

"git fetch" learned "--set-upstream" option to help those who first clone from their private fork they intend to push to, add the true upstream via "git remote add" and then "git fetch" from it.

You can follow along the discussions here.

See commit 24bc1a1 (19 Aug 2019) by Corentin BOMPARD (``).
(Merged by Junio C Hamano -- gitster -- in commit 9437394, 09 Sep 2019)

pull, fetch: add --set-upstream option

Add the --set-upstream option to git pull/fetch which lets the user set the upstream configuration (branch.<current-branch-name>.merge and branch.<current-branch-name>.remote) for the current branch.

A typical use-case is:

git clone http://example.com/my-public-fork

git remote add main http://example.com/project-main-repo git pull --set-upstream main master

or, instead of the last line:

git fetch --set-upstream main master

git merge # or git rebase

This is mostly equivalent to cloning project-main-repo (which sets upsteam) and then "git remote add" my-public-fork, but may feel more natural for people using a hosting system which allows forking from the web UI.

This functionality is analog to "git push --set-upstream".


Note: that last feature introduced a type, fixed with Git 2.25 (Q1 2020).

See commit 391c7e4 (31 Oct 2019) by Ralf Thielow (ralfth).
(Merged by Junio C Hamano -- gitster -- in commit 7ab2088, 01 Dec 2019)

fetch.c: fix typo in a warning message

Signed-off-by: Ralf Thielow
Reviewed-by: Jonathan Nieder

So it is not:

multiple branch detected, incompatible with --set-upstream

But:

multiple branches detected, incompatible with --set-upstream

Note: With Git 2.27 (Q2 2020), the documentation has been updated.

See commit 9c68873 (09 Mar 2020) by René Scharfe (rscharfe).
(Merged by Junio C Hamano -- gitster -- in commit ab8ef92, 25 Mar 2020)

pull: document more passthru options

Signed-off-by: René Scharfe

git pull accepts the options --dry-run, -p/--prune, --refmap, and -t/--tags since a32975f516 ("pull: pass git-fetch's options to git fetch", 2015-06-18, Git v2.6.0-rc0 -- merge listed in batch #0), -j/--jobs since 62104ba14a (submodules: allow parallel fetching, add tests and documentation, 2015-12-15, Git v2.8.0-rc0), and --set-upstream since 24bc1a1292 (pull, fetch: git add --set-upstream option, 2019-08-19, Git v2.24.0-rc0). Update its documentation to match.


Note: With Git 2.29 (Q4 2020), the --set-upstream option is clearer:

See commit 847b372 (12 Aug 2020) by Philippe Blain (phil-blain).
(Merged by Junio C Hamano -- gitster -- in commit ee356a8, 19 Aug 2020)

fetch, pull doc: correct description of '--set-upstream'

Signed-off-by: Philippe Blain

The '--set-upstream' option to git fetch(man) (which is also accepted by git pull(man) and passed through to the underlying git fetch(man)) allows setting the upstream configuration for the current branch.

This was added in 24bc1a1292 (pull, fetch: add --set-upstream option, 2019-08-19, Git v2.24.0-rc0).

However, the documentation for that option describes its action as 'If the remote is fetched successfully, pull and add upstream (tracking) reference [...]', which is wrong because this option does not cause neither git fetch nor git pull to pull:

Fix the description of that option.
If the remote is fetched successfully, add upstream

The documentation now includes:

If the remote is fetched successfully, add upstream (tracking) reference, used by argument-less git pull and other commands.

It removes the notion of pulling and then adding upstream.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250