160

Alright I did a little bit of research on this but I couldn't find an exact answer, so I have to ask.

I have 2 remotes: origin and repo2.

I'd like to do something like

git pull repo2 master

But this would pull the master branch of the repo2 into my master. Can I specify a different branch to pull into, for my local branch ?

Cosmin Atanasiu
  • 2,532
  • 3
  • 21
  • 26

2 Answers2

247
git checkout -b myBranchName repo2/master
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 30
    Note - you have to have run `git fetch remote2` beforehand, if not done yet. Otherwise you may see > fatal: Cannot update paths and switch to branch 'myBranchName' at the same time. – dman Jan 26 '16 at 01:38
  • 2
    Note - if you need to push back, use `git push repo2 myBranchName:master` as a simple `git push` will fail – jaimedash May 04 '16 at 19:48
  • 3
    What if the branch already exists? I want to update code there with the code from repo. – Jared Nov 24 '17 at 20:40
  • @Jared Technically a different question, but also easily possible (and actually thats one of the most common use-cases). It slightly depends on what you already did. In most cases it's just `git checkout my_branch && git pull --rebase` (`--rebase` depends on your workflow). If the branch is not already tracking the remote one, you need to execute `git branch --set-upstream my_branch upstream/my_branch` once. – KingCrunch Nov 25 '17 at 21:58
4

The git pull command is a convenience function that does git fetch and git merge. If you only want retrieve branches from a new remote without trying to merge it into any working copy branch you can just use git fetch. You can then refer to git branch -av to see all the local and remote branches and operate on either remote as you like.

Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
Ben Jackson
  • 90,079
  • 9
  • 98
  • 150