Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 623
Misha Moroshko Avatar asked Jan 16 '12 09:01

Misha Moroshko


People also ask

Can you checkout a remote branch in git?

In order to checkout a remote branch you have to first fetch the contents of the branch. In modern versions of Git, you can then checkout the remote branch like a local branch. Older versions of Git require the creation of a new branch based on the remote .

How do I fetch a remote branch?

To view your remote branches, simply pass the -r flag to the git branch command. You can inspect remote branches with the usual git checkout and git log commands. If you approve the changes a remote branch contains, you can merge it into a local branch with a normal git merge .


1 Answers

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 
like image 165
Bill Door Avatar answered Sep 23 '22 08:09

Bill Door