400

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

Cascabel
  • 479,068
  • 72
  • 370
  • 318
Rachel
  • 100,387
  • 116
  • 269
  • 365
  • 2
    calmh pretty much has it covered, but the non-answer is that you shouldn't ever do `git pull origin/master`. If you want to merge the [locally stored] remote branch `origin/master`, just use `git merge origin/master`. – Cascabel May 21 '10 at 17:10
  • 3
    @Jefromi: Can you explain as to why it is always better to do git merge as compared to git pull ? – Rachel May 21 '10 at 17:14
  • 28
    `git pull` means `git fetch` followed by `git merge`. It fetches the content from the remote, then merges it into your current branch. But `origin/master` is a local branch (tracking a remote branch). If you want to merge it, you don't need to fetch anything. It's misleading to say `git pull origin/master` when you're not actually fetching from a remote. – Cascabel May 21 '10 at 17:24
  • Thanks Jefromi for the useful information. It really helps to understand pretty easily not so easy concept. – Rachel May 21 '10 at 17:39
  • 3
    For those reading this and still confused, `origin/master` is a locally stored branch that caches the master branch at the origin remote. – iheanyi Dec 14 '18 at 18:16

3 Answers3

531

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

git pull origin/master will pull changes from the locally stored branch origin/master and merge that to the local checked-out branch. The origin/master branch is essentially a "cached copy" of what was last pulled from origin, which is why it's called a remote branch in git parlance. This might be somewhat confusing.

You can see what branches are available with git branch and git branch -r to see the "remote branches".

Jakob Borg
  • 23,685
  • 6
  • 47
  • 47
  • 1
    in case of `git pull origin master` will it always merge to the master branch, lets say am on another branch in my repo and then am doing above command, will it update my current branch with the origin remote changes or my master branch with the changes ? – Rachel May 21 '10 at 16:38
  • My answer was actually a bit incorrect. :) I've updated it. The answer to your question is that in either case, it will merge to your current branch. To avoid merging with your current branch you need to `git fetch` and the separately `git merge`. – Jakob Borg May 21 '10 at 16:55
  • 5
    @calmh: `git merge` (and therefore `git pull`) always merges into the current branch. To merge with something other than your current branch, just check it out first. – Cascabel May 21 '10 at 17:10
  • 2
    um .. I don't see how 'origin/master' is any different from 'origin master'; they're both the master branch on origin. Can you actually give an example of when they would be different? – hasen May 23 '10 at 21:39
  • 1
    @hasen Try doing a "pull origin master" when you are disconnected from the network. Then try "merge origin/master" (you can't pull, since it's not a remote). One works, because it's local, and one doesn't. – Jakob Borg May 23 '10 at 21:48
  • 89
    `git pull origin/master` may have been a valid command when this was written, but nowadays (git 1.7.10.3) it fails with `fatal: 'origin/master' does not appear to be a git repository` (as it should - pull is always for pulling from remotes). – user1338062 Aug 06 '12 at 07:48
  • 1
    so whats the difference between 'git pull origin/master' and 'git merge master' when you are on a branch? - @Jakob Borg – Srikanth Kondaparthy Dec 18 '14 at 01:22
  • 6
    Why is git so confusing? So we have 4 repositories totally, correct? There is a (1) remote repository, (2) a local repository, (3) a staging repository, (4) a local-remote aka origin/master? Why would git have #4 repository at all? – Mugen Jul 28 '19 at 14:25
  • 2
    @Rachel, to answer your question, the term "master" in "git pull origin master" is referring to the source (not destination) branch; i.e., it will pull new changes from a branch named "master", on the remote named "origin" (default alias for the remote repo URL from which your HEAD branch was cloned), and then merge those changes into your local HEAD branch, i.e. the local branch that was "active"/checked out when u issued the command. – galaxis May 07 '21 at 20:54
  • Note that you will need to use `git pull origin main` (not master) for all new Git repositories on GitHub starting October 1, 2020. – cristiandatum Jun 18 '21 at 10:29
13

git pull origin master will fetch all the changes from the remote's master branch and will merge it into your local. We generally do not use git pull origin/master. We can do the same thing by git merge origin/master. It will merge all the changes from "cached copy" of origin's master branch into your local branch. In my case, git pull origin/master is throwing the error:

fatal: 'origin/master' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
questionto42
  • 7,175
  • 4
  • 57
  • 90
Dhruvil Shah
  • 376
  • 2
  • 8
13

git pull = git fetch + git merge origin/branch

git pull and git pull origin branch only differ in that the latter will only "update" origin/branch and not all origin/* as git pull does.

git pull origin/branch will just not work because it's trying to do a git fetch origin/branch which is invalid.

Question related: git fetch + git merge origin/master vs git pull origin/master

user33276346
  • 1,501
  • 1
  • 19
  • 38