18

Why does git pull get all branches from repository but git pull origin master doesn't? I discovered it the hard way. Is it the only functional difference between these two commands?

Explanation like this tells me nothing:

git pull = git fetch origin + git merge origin/master

git pull origin master = git fetch origin master + git merge FETCH_HEAD

Eric
  • 87,154
  • 48
  • 211
  • 332
Prostak
  • 2,925
  • 7
  • 32
  • 44

3 Answers3

38

The latter command, git pull origin master, tells git to fetch and merge specifically the master branch (from the remote named origin, to be even more precise).

git pull fetches updates for all local branches, which track remote branches, and then merges the current branch.

esycat
  • 1,204
  • 10
  • 10
  • So "fetches updates for all local branches" is the difference. If it fetches updates for all local branches without merging, what does it actually do? – Prostak Jul 05 '13 at 01:09
  • 4
    `.git` directory in your local clone contains all objects, which you have already received from the remove repository. `git fetch` is basically a command to receive any new objects from the remote. This way, all other operations, including merging (but excluding `git push`, of course), work locally (and do not require network connection) – esycat Jul 05 '13 at 04:52
9

From the documentation of git pull:

git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch

When you call git fetch without arguments, the following happens

Fetches named heads or tags from one or more other repositories, along with the objects necessary to complete them.
git fetch [fetches] from (...) a single named repository (...)

When you add arguments, only the specified remote and head (=branch/tag/commit/...) are fetched, and then merged.

phihag
  • 245,801
  • 63
  • 407
  • 443
  • "Fetches named heads or tags from one or more other repositories"...hmm... I thought there is only one HEAD and it is always on master branch? What does it mean "heads"? – Prostak Jul 05 '13 at 01:11
  • 1
    `HEAD` is the current commit. `heads` is a term meaning basically `branches and tags`. – phihag Jul 05 '13 at 01:12
  • 1
    The term `HEAD` is used mostly to refer to the last commit on your current branch. But, apparently, each branch and tag at any point in time has its own last commit, aka tip. `HEADS` is a term that is usually used to refer to all these tips. – esycat Jul 05 '13 at 05:03
  • 1
    Just a heads up, the behavior of `git pull` without arguments also depends a lot on how Git is configured, see "Default Behavior" section of https://www.kernel.org/pub/software/scm/git/docs/git-pull.html. –  Jul 07 '13 at 13:59
-1

Git pull only pulls the checkout branch.

If you want to update all local branches with origin remote branches.

git pull --all

Can "git pull --all" update all my local branches?

Michiel
  • 25
  • 8
  • Maybe you meant "git pull --all" (no space). The git manpage is ambiguous about that, but according to my testing that only updates all your local branches that are already set up to track remote branches. See this other answer for more info on how to get all the remote branches that are available: https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches – Joshua Richardson Dec 05 '20 at 01:25