89

What are the differences between these commands?:

# 1
git pull
# 2
git pull origin
# 3
git pull origin master
# 4
git pull origin/master
# 5
git pull origin HEAD:master
Ben Gao
  • 1,707
  • 1
  • 13
  • 10
  • Well, even after reading the man page, It is not in all cases clear what exactly happen. For example: What is the meaning of `git pull` without a configured upstream? (The man page only states the default to be the configured upstream.) – michas Apr 03 '13 at 21:44
  • In short: 1. will fail without a configuration for the current branch and will otherwise be like 2. with the remote name. 2. will use the default fetch configurations for the given remote (and merge the first one), whereas in 3. you specify what to fetch and merge. 4. is invalid, imho. 5. (if it works), will put the remote HEAD into refs/remotes/origin/master and merge that. – andi5 Apr 03 '13 at 22:13
  • 2
    Voting to reopen this question because, unfortunately, it's one of the search results for [`site:stackoverflow.com git Difference "git pull" "git pull origin master"`](https://www.google.com/search?q=site%3Astackoverflow.com+git+Difference+%22git+pull%22+%22git+pull+origin+master%22). –  May 30 '14 at 13:41

2 Answers2

89

git pull is a convenience command, which is doing different things at the same time. Basically it is just a combination of git fetch, which connects to the remote repository and fetches new commits, and git merge (or git rebase) which incorporates the new commits into your local branch. Because of the two different commands involved the meaning of git pull is not always obvious.

You can configure an upstream for a local branch. After a fresh clone you will have a local branch "master", a remote "origin" and your master branch has "origin/master" as upstream. I assume this setup below. (You can see your upstream configuration with git branch -vv or by looking at .git/config.)

Now for your questions:

  1. git pull= git fetch origin + git merge origin/master (or whatever your upstream is)
  2. git pull origin = git pull (as long as origin is your upstream remote)
  3. git pull origin master = git fetch origin master+git merge FETCH_HEAD
  4. git pull origin/master : invalid unless you have a remote called "origin/master"
  5. git pull origin HEAD:master : Tries to directly reset you local master to whatever HEAD points to on origin. (Don't do this.)
michas
  • 25,361
  • 15
  • 76
  • 121
  • 2
    Why is executing `git pull origin HEAD:master` a bad idea? – Ryan Edwards Apr 03 '13 at 22:49
  • 2
    The right side is supposed to be a remote branch. See warning in man page. Be sure you know what you are doing if you use this. – michas Apr 03 '13 at 23:06
  • if i was in someother branch would `git pull` pull that branch or master – aWebDeveloper Feb 07 '14 at 09:37
  • 1
    @aWebDeveloper: For completeness: `git pull origin HEAD:master` essentially (was literally until the script was rewritten in C in Git 2.6) passes the `HEAD:master` part to `git fetch`, so that does what `git fetch` does for that step; then merges or rebases using the commit hash the `git fetch` step got. Refspecs are *source:dest*, so `HEAD` is given to the other Git to translate. So it's up to the other Git—but usually the other Git's `HEAD` is a name for its `master`. If you're not on your own `master`, the merge-or-rebase uses your fetch-updated `master` (*dest*) (unless the `fetch` fails). – torek Mar 15 '17 at 13:04
  • 1
    One other thing to be careful about: never do `git pull origin br1 br2`. It looks and feels like this should `git checkout br1; git pull origin; git checkout br2; git pull origin`—but it doesn't! Instead it, in effect, does: `git fetch origin && git merge origin/br1 origin/br2`, which merges *both* fetch results into your *current* branch, something Git calls an *octopus merge*. This is never what anyone wants. Probably `git pull` should reject the command entirely (anyone who really *does* want it can run fetch first, then merge). – torek Mar 15 '17 at 13:06
  • When I did a simple `git pull` some newly created branches on the remote repository got downloaded to my local repository. I thought `git pull` only fetches and merges changes from the currently checked out branch? – aniztar Feb 08 '19 at 06:43
  • @aniztar: `git pull` first runs `git fetch`. So `git pull` by itself runs `git fetch` by itself, which fetches everything. (The details may vary slightly from one Git release to another, due to the rewrite from shell script to C code.) It's always safe to run `git fetch`, in a normal Git repository, so this is harmless. – torek Feb 26 '19 at 16:00
  • What does `git merge FETCH_HEAD` do? – Ollie Williams Jun 07 '22 at 03:57
18

A pull is basically a fetch (which gets some commits and associated objects from a remote repository into yours) and then an operation which "applies" these into your working copy. The second stage is, by default, done using a merge but you can set the pull.rebase variable to true and then it will rebase instead.

There are two questions that pop up with the pull command. The first is, what exactly gets fetched? And the second is, how does it apply these changes to my working copy? Let's start with the first. The full form of the command is

git pull [options] [repository] [<refspec>...]

The options are flags that control behaviour (e.g. --rebase to make pull work as a fetch + rebase even if pull.rebase is false).

repository is the name (or URL) of the remote to fetch from.

refspecs are a succinct way of specifying which references on the remote you want to fetch and where do you want to put them in your current working copy.

Let's take the most explicit form first.

 git pull origin branch1:branch2

This basically says, pull the changes in the reference branch1 on the remote called origin and then merge (or rebase) them into the local branch branch2. If I, for example, say git pull origin master:dev, I will get a local branch called dev which will point to the same commit as master. The details of how to specify refspecs are here. You can use a * to indicate multiple refspecs. For example, git pull origin refs/heads/*:refs/heads/* will pull all the branches (stored under heads) into the local repository and merge them into local branches with the same names.

Now, let's remove arguments one by one to discuss how the default work. First, we can remove the destination from our refspec and simply say git pull origin branch1. This will first fetch the remote branch branch1 into your local repository. It will be available as a temporary reference called FETCH_HEAD. After that, it will run git merge FETCH_HEAD which will merge this branch into your current active branch (i.e. HEAD). This is often done when you're in a local branch and want to fetch changes from a remote into that branch.

Now, let's drop the branch1 completely and just say git pull origin. Now, git knows where to fetch from (origin) but doesn't know what to fetch. It has some defaults for this. The most scenario is when your config file has a branch.<name>.merge option (this is an entry called merge inside a section like [branch "master"]). If so, it will use the refspecs there for the operation.

If we drop the origin completely and simply say git pull, it will check the config to see if there is a branch.<name>.remote which specifies which remote to pull from. That along with the above tells you what to pull.

Your points #4 and #5 are not normal use cases. The first makes sense if you have a remote called origin/master which is unlikely. origin/master is usually a local reference that tracks the master branch on the remote origin. The second will try to fetch changes on HEAD on the remote (the default branch which is usually master) and then merge those into your local master. While this might be something you want to do on a regular basis, the command is quite unconventional and not something I've seen used often.

I've skipped a few details but these should be enough to keep you safe and comfortable in your daily work. For all the gory details, you can check out the manual page for git pull.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • "If I, for example, say git pull origin master:dev, I will get a local branch called dev which will point to the same commit as master." So you would download a new branch called dev and it would point to master? – user33276346 Oct 18 '19 at 12:36
  • No, you would get the branch `master` from the remote but it would be called `dev` locally. – Noufal Ibrahim Oct 20 '19 at 16:47
  • `git pull origin refs/heads/*:refs/heads/*` didn't work for me, and I got `no matches found: refs/heads/*:refs/heads/*`. I also tried `git pull origin refs/remotes/origin/*:refs/heads/*`, but that didn't work either. I don't think pulling all branches on remote in one command is possible, and neither is the subsequent merging/ rebasing of all these branches into their subsequent local branches. – Ben Butterworth Oct 22 '20 at 21:53