26

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
  • 95,302
  • 53
  • 242
  • 374
Prostak
  • 3,565
  • 7
  • 35
  • 46

3 Answers3

50

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,324
  • 11
  • 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
  • 5
    `.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
  • What exact commands are executed at `git pull origin master`? What happens if I'm currently on another local branch then `master`? – Ini May 20 '22 at 08:59
  • 1
    @Ini It'll fetch and merge the remote branch master in the branch you're at the moment. So if you are working in your branch develop and git pull origin master, it will fetch all new stuff master has in remote and merge into your local develop. – Gaspar Feb 09 '23 at 17:18
10

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
  • 278,196
  • 72
  • 453
  • 469
  • "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
  • 2
    `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
  • What does it mean to say that "each tag has its own last commit" @esycat? I understand HEAD in the context of a branch, but I thought until now that a given tag is for a single commit. When you say last commit, it seems to imply that a tag can have a series of commits, which is confusing. Can you clarify? – AllSolutions Jun 01 '23 at 13:42
-3

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
  • 161
  • 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