Misha Moroshko
Misha Moroshko

Reputation: 171479

How to checkout a remote branch in Git?

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?)

Upvotes: 48

Views: 184340

Answers (5)

Arqam Rafay
Arqam Rafay

Reputation: 135

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.

Upvotes: 0

Mukesh Chapagain
Mukesh Chapagain

Reputation: 26016

git fetch && git checkout new_feature_branch

Upvotes: 6

Adam Dymitruk
Adam Dymitruk

Reputation: 129782

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.

Upvotes: 43

Bill Door
Bill Door

Reputation: 18966

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

Upvotes: 62

Troels Thomsen
Troels Thomsen

Reputation: 11627

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.

Upvotes: 16

Related Questions