kjo
kjo

Reputation: 35301

How to pull new branch and set up tracking simultaneously?

Suppose that a new branch NEW has just appeared in a remote repo. Is there a git command to create a tracking branch for NEW, and simultaneously pull it (specifically) from the remote repo? (By the "specifically" bit I mean pull only the branch NEW.)

FWIW: I'm using version 1.7.6.

EDIT: desired scenario

(before)

% git branch -a
  bar
  baz
  foo
* master
  remotes/somerepo/bar
  remotes/somerepo/baz
  remotes/somerepo/foo
  remotes/somerepo/master

(after)

% git branch -a
  bar
  baz
  foo
* master
  NEW
  remotes/somerepo/bar
  remotes/somerepo/baz
  remotes/somerepo/foo
  remotes/somerepo/master
  remotes/somerepo/NEW

Upvotes: 1

Views: 2863

Answers (2)

janos
janos

Reputation: 124648

You can do that with:

git branch NEW somerepo/NEW

This will create local branch NEW, set up to track remote branch NEW from somerepo.

The thing about git pull is that it's a combo of git fetch and get merge. Since git merge can only work on the current branch, you cannot use git pull for this purpose. If you want to fetch a specific branch and create a local branch to track it, you have to use the commands:

git fetch remote branchname
git branch branchname remote/branchname

In older versions of Git, the git fetch remote branch might not create .git/refs/remotes/remote/branchname correctly. I tested this works as of version 1.8.4, but it doesn't work as of version 1.7.10.4. If it doesn't work with your version, you have to use this more verbose syntax:

git fetch remote branchname:remotes/remote/branchname

Or if you don't mind fetching all the branches, you could do simply git fetch remote.

Upvotes: 2

David
David

Reputation: 7303

Edit:

This is probably what you want:

git checkout -tb NEW somerepo/NEW

Upvotes: 1

Related Questions