65

What is the difference between these two commands?

git pull

and

git pull origin master
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

3 Answers3

46

[Edit, May 2018: git pull is no longer a shell script and a few details are different in modern Git. Pull also now has recursion options that make it more useful with submodules. This answer ignores the submodules.]

The git pull script is meant as a convenience method for invoking git fetch followed by git merge (or, with git pull --rebase, invoking git fetch followed by git rebase).

The first extra argument to git pull tells it which remote to give to the fetch operation:

git pull origin

for example, means to fetch from origin. If you leave this out, Git uses the current branch's remote:

$ git branch
* master
$ git config --get branch.master.remote
origin

The second (and any additional) arguments to git pull tell it which branch or branches to merge in. These are the names of the branches as found on the remote. For instance, suppose you create a new branch feature2 that tracks origin/feature:

$ git checkout -b feature2 origin/feature

If you now want to fetch from origin to pick up new commits added to their feature branch, but merge them in to your local feature2 branch:

$ git pull origin feature

If you leave out the branch name(s), git uses the current branch's merge:

$ git config --get branch.feature2.merge
feature

Note that if you list multiple branch names, Git will do an "octopus merge". In my experience, this usually surprises people the first time: they think git pull remote br1 br2 will run git fetch followed by a series of separate git merge-s on each branch, but that's not what happens.

torek
  • 448,244
  • 59
  • 642
  • 775
  • as always awesome explanation. But in the recent version I see `git pull origin` updates all the remote tracking branches ? – Number945 May 23 '18 at 09:09
  • 1
    @BreakingBenjamin: that's certainly possible, if someone decided that `git pull origin` should run `git fetch origin` rather than `git fetch origin `. Now that `git pull` is written in C rather than shell script it's harder to inspect its interior details, and Git is always evolving. From a quick scan of the source, it appears now that `git pull origin` runs `git fetch origin`, which will fetch everything. I'll update this answer (which is from 2014). – torek May 23 '18 at 14:23
21

git pull origin master will pull changes from the origin remote, master branch and merge them to the local checked-out branch.

where as git pull will fetch new commits from all tracked branches from the default remote(origin). you can also configure default remote and branch name in gitconfig file.

git branch --set-upstream master origin/master

This will add the following info to your gitconfig file:

[branch "master"]
    remote = origin
    merge = refs/heads/master

now whenever you say git pull it will fetch from origin master.

nitesh goel
  • 6,338
  • 2
  • 29
  • 38
9

git pull origin master is the verbose form which specifies both the remote (origin) and the branch to pull from (master). If you don’t specify that, Git will apply a default behavior that is explained in the documentation:

Often people use git pull without giving any parameter. Traditionally, this has been equivalent to saying git pull origin. However, when configuration branch.<name>.remote is present while on branch <name>, that value is used instead of origin.

[…]

In order to determine what remote branches to fetch (and optionally store in the remote-tracking branches) when the command is run without any refspec parameters on the command line, values of the configuration variable remote.<origin>.fetch are consulted, and if there aren't any, $GIT_DIR/remotes/<origin> file is consulted and its Pull: lines are used.

poke
  • 369,085
  • 72
  • 557
  • 602