27

What is the difference between git pull and git pull origin master?

What if I am on a branch other than master, will the two commands achieve a different result?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kgbph
  • 841
  • 1
  • 8
  • 26
  • 4
    If you are new to Git, I recommend *avoiding* `git pull` for some time. First, get yourself familiar with both `git fetch` and `git merge`. If you plan to use `git rebase`, get familiar with that too. Then, later, you can use `git pull` as a convenience command, knowing that what it does is run `git fetch` with some arguments, followed by either `git merge` or `git pull` with additional arguments. Once you're familiar with each of the two commands, the whole thing put together will also make sense. Until then, though, it won't! – torek Aug 31 '18 at 05:55
  • 1
    That said, the short answer to your question is that they are usually different, if you are not on `master` now. – torek Aug 31 '18 at 05:56

4 Answers4

41

Remember, a pull is a fetch and a merge.

  • git pull origin master fetches commits from the master branch of the origin remote (into the local origin/master branch), and then it merges origin/master into the branch you currently have checked out.

  • git pull only works if the branch you have checked out is tracking an upstream branch. For example, if the branch you have checked out tracks origin/master, git pull is equivalent to git pull origin master

Naga
  • 800
  • 7
  • 8
12

First, let us understand what git pull is:

  • The git pull command is used to fetch and download content from a remote repository and immediately update the local repository to match that content. The git pull command is a combination of git fetch and git merge. git pull will download the content from the remote repository. Once the content is downloaded, git merge will merge the content to your local repository. A new merge commit will be created and HEAD updated to point at the new commit.

  • Now that we know what git pull does, when we do git pull origin master, it simply fetches a copy of the master branch from the original repository, and merges it with the current branch that you have checked out.

For more information, you can go to this link.

rooni
  • 1,036
  • 3
  • 17
  • 33
sana
  • 763
  • 9
  • 19
3

From the documentation:

https://git-scm.com/docs/git-pull

git pull [<options>] [<repository> [<refspec>…​]]

git-pull - Fetch from and integrate with another repository or a local branch

...

Default values for and are read from the "remote" and "merge" configuration for the current branch as set by git-branch[1] --track.

So

  1. If your branch is set to "master", then git pull and git pull origin master will do the same thing.

  2. If your branch is set to "master", Git pull and git pull origin some-other-branch will be different

  3. If your branch is set to "some-other-branch", then Git pull and git pull origin master will be different

paulsm4
  • 114,292
  • 17
  • 138
  • 190
3

Git pull = Git fetch + Git merge.

git pull origin master Let's say you are on local/master, and run this command, git will fetch commits from origin/master and then merge it into local/master.

git pull This is a shorthand for pulling commits into local branch that is tracking a remote branch.

And that brings the question, how does one make a local branch track a remote branch.

As far as I know, there are two common ways to do so:
1. When pushing for the first time: git push -u origin branch_name The -u flag tells git to make the local branch track the remote branch.

  1. When creating a local branch for an existing remote branch: git branch --track branch_name origin/branch_name
Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23