46

Someone pushed a "new feature" branch to the shared repo:

git push -u new_feature_branch

Now, I would like to create a copy of this branch on my local machine in order to test the new feature.

What would be the easiest way to do this? (Do I need to fetch / pull before checkout?)

Charles
  • 50,943
  • 13
  • 104
  • 142
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
  • Might be duplicated. http://stackoverflow.com/questions/1783405/checkout-remote-git-branch – Harvey Pham Sep 22 '15 at 22:02
  • 3
    Possible duplicate of [How do I check out a remote Git branch?](https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch) – Rahul Jun 15 '17 at 10:15

5 Answers5

59

I generally find it unnecessary to use git fetch. git pull is sufficient. git pull will synchronize your repository with the remote. The new_feature_branch will then be available.

git checkout new_feature_branch will notice the branch in origin and create a new local tracking branch for you and switch to that branch.

git pull
git checkout new_feature_branch
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
Bill Door
  • 18,272
  • 3
  • 32
  • 37
42

The simplest way to do this is:

git fetch
git checkout -t origin/new_feature_branch

This is only done initially. From now on you can continue working with the branch as you do for the others you use.

Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
16

You need to fetch upstream changes so your local repository includes the relevant objects (git fetch --all or git fetch <remote>).

Afterwards you can perform a checkout using git checkout <branch> (if you like to do it explicitly, you can type git checkout -b <branch> <remote>/<branch>; the local name doesn't have to be the same as the remote). If you don't already have a local branch of that name, it will checkout the remote branch and track it.

As an alternative to this, you can use git pull <remote> <branch>, but this will - with default settings - merge the remote branch into your current, which is probably not what you want.

Troels Thomsen
  • 11,361
  • 4
  • 28
  • 29
  • I wonder why this answer says that you MUST use `fetch --all` and the top answer just mentions `fetch`. Which one is it? – Petri Sirkkala Jul 18 '17 at 13:27
  • 1
    @PetriSirkkala The answer does not say that you must use `--all`. `fetch --all` says fetching all remotes (if you have more than e.g. `origin`). – Roi Danton Dec 11 '17 at 14:38
5
git fetch && git checkout new_feature_branch
Mukesh Chapagain
  • 25,063
  • 15
  • 119
  • 120
  • can you please look into this question if you have time. http://stackoverflow.com/questions/20634111/magento-error-when-disable-module – Tahir Yasin Nov 12 '15 at 08:24
0

for synchronize your repository with the remote branch run git fetch then after fetching run git checkout new_branch_name if you are using vs code add git Graph extension for diagnose the issue. It will help you in future use as well.