3

I need to be able to pull to a branch that I might not be on (in fact, assume for the sake of argument I don't know what branch I'm on). For example, I have a remote origin with a branch master that is tracked locally in the usual way by a local master using a tracking branch. Are there options or arguments I can supply to git-pull that will have the same effect as switching to master, pulling, and then switching back to whatever branch I started on?

Many answers here suggest that this is not possible, but the documentation's discussion of <dst> under <refspec> suggests that something like

git pull origin master:master 

will do the trick. Is that so?

orome
  • 45,163
  • 57
  • 202
  • 418
  • It's what I use... " git pull origin mySourceBranch" – BENARD Patrick Dec 31 '13 at 14:49
  • @Jahnux73: What is `mySourceBranch` in my scenario? Is it `master`, `master:master`, `remotes/origin/master` — or something else. I want the effect to be the same as if I'd pulled after switching to `master` (which has the usual tracking set up). – orome Dec 31 '13 at 14:54
  • When I'm on branch 'feature' , when i make a 'git pull origin develop', it make a pull of 'develop branch' on my actual branch 'feature'. ( so master for you i think) – BENARD Patrick Dec 31 '13 at 14:58

4 Answers4

5

A git pull basically does a git fetch followed by a git merge.

Doing git pull for another branch is not possible, because doing git merge for another branch is not possible. To be able to do git merge, you need to checkout the target branch first. For this same reason, you cannot do git pull for another branch.

@torek added an excellent point:

And, to specifically address git pull origin x:y: git pull passes the x:y argument to git fetch, so git fetch does what it does; but then git pull attempts to merge into the current branch, regardless of x and/or y.

See this also for reference:

Merging Branches Without Checkout

Community
  • 1
  • 1
janos
  • 120,954
  • 29
  • 226
  • 236
  • 2
    And, to specifically address `git pull origin x:y`: `git pull` passes the `x:y` argument to `git fetch`, so `git fetch` does what it does; but then `git pull` attempts to merge into the current branch, regardless of `x` and/or `y`. – torek Dec 31 '13 at 17:14
1

I've never seen the master:master syntax. Generally: git pull origin e.g. master.

I think what you want to do is do a fetch first. Then you have the ability to merge any of the tracking branches, or switch to them.

Rob
  • 11,446
  • 7
  • 39
  • 57
  • Nor had I, so I was surprised to see it in the docs — and surprised to see that it seemed to do what I wanted. (Remember I'm not asking about `fetch`: I'm asking specifically whether I can use `pull` in this way.) – orome Dec 31 '13 at 14:52
0

A pull is basically a fetch followed by a checkout of the updates on the current (tracking) branch. All you want is the fetch.

git fetch origin master:master
Dipstick
  • 9,854
  • 2
  • 30
  • 30
  • 1
    No, I really want the `pull`, but I see the problem with my question now: I'm trying to get the working tree files updated without switching branches. That makes no sense, right? – orome Dec 31 '13 at 15:00
0

Do you really need the merge feature of git pull?

If not, you could simply do:

git fetch
git branch -f master origin/master

This simply makes the local master to point on the same commit, the remote master points to.

michas
  • 25,361
  • 15
  • 76
  • 121