Introduction
In this tutorial we are going to explain an important concept when using Git. In particular we will try to show you the difference between local branch and remote branches.
Git is distribuited and it could have several remote branches.
We will try to explain more than one alternative how to checkout remote branches.
Context
Supose there is a pushed a branch called test
which was created with git push origin test
to a shared
repository.
You can use git branch -r
to verify that the remote git branches exists.
Solution 1: Fetching and checkout
To use the remote branch test
you can just do:
git fetch
git checkout test
Important: git checkout test
will NOT work in newer git if you have multiple remotes.
If you are using a newer git version try:
git checkout -b test origin/test
Solution 2: Alternative way of fetching and checkout
To fetch a branch, you simply need to:
git fetch origin
This will fetch all of the remote branches for you. You can see the branches available for checkout with:
git branch -v -a
With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy: git checkout -b test origin/test
Create a local branch and fetch
In this case, you need to create a local test
branch which will
track the remote test
branch:
$ git branch test origin/test
Common Error: fatal: git checkout: updating paths is incompatible with switching branches
If you are trying to checkout a remote branch using -b and from origin you could get the following error:
$ git checkout -b remote_branch origin/remote_branch
fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/remote_branch'
If you receive this message, you must first do a:
git fetch origin
git checkout
remote_branch