Git pull - default remote and branch using -u opti

2020-06-23 07:44发布

问题:

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?

回答1:

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.



回答2:

-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



回答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.



标签: git github