27

I've been searching for a half hour for how to do pull a remote branch, and cannot figure it out.

My remote git repository has a branch called frontend. When I run git pull origin frontend I get the following output:

* branch             frontend     -> FETCH-HEAD
Already up-to-date.

When I run git branch, I get the following:

*master

Why isn't frontend in the list returned by git branch?


When I run git branch -v -a, one of the returned branches is remotes/origins/frontend...

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Christopher Shroba
  • 7,006
  • 8
  • 40
  • 68
  • The question in the title ("How do I pull down a remote branch?") is a separate question from the one in the body of the text ("Why isn't frontend in the list returned by git branch?"). When you did the `git pull` you did pull down the remote branch. That relates to the question in the title. There are any number of ways for you to create a local branch named "frontend" and some of those are described in answers below. Those address the question in the body. – Jeff Scott Brown Nov 08 '14 at 02:15

3 Answers3

33

git pull origin frontend is equivalent to get fetch origin frontend and get merge frontend. Note that this merges the remote branch named frontend to the current local branch, in your case master. If you want a local branch with the same name as the remote branch, you should create it first. One way to do this is

git checkout -b frontend
git pull origin frontend

You should read up on the differences between a local branch and a remote tracking branch.

Alternatively, you can manually fetch then checkout the branch:

git fetch origin
git checkout frontend

If you don't already have a branch with the name frontend, git will find the remote tracking branch and automatically create a local branch at the same commit.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 2
    There's one other subtle difference (that's gone in the newest git) when separating out the fetch-and-merge: when you run `git fetch `, older versions of git fetch all the named branches but put them *only* under the special name `FETCH_HEAD`, rather than updating your local copies of the remote's branches. This means `git merge frontend` doesn't do what you wanted. Newer versions update the "remote branches", so that the `git merge frontend` works as desired here. My general rule is "avoid git pull" :-) which simply sidesteps the whole "which git version" question. – torek Nov 08 '14 at 02:28
7

You need to create local branch as well. Since there already is a remote branch in your repo, just type:

git checkout frontend

git will autocratically create local branch and set upstream branch as well.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
5

Why isn't frontend in the list returned by git branch?

Because you don't have a local branch named "frontend".

There are a number of git config settings that might factor in here but one thing you can do if you want to create a local branch named "frontend" which tracks the remote branch of the same name is something like the following:

git branch --track frontend origin/frontend

I hope that helps.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47