While the first and selected answer is technically correct, there’s the possibility you have not yet retrieved all objects and refs from the remote repository. If that is the case, you’ll receive 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’ which can not be resolved as commit?
Solution
If you receive this message, you must first do a git fetch origin
where origin
is the name of the remote repository prior to running git checkout remote_branch
. Here’s a full example with responses:
$ git fetch origin
remote: Counting objects: 140, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 69 (delta 36), reused 66 (delta 33)
Unpacking objects: 100% (69/69), done.
From https://github.com/githubuser/repo-name
e6ef1e0..5029161 develop -> origin/develop
* [new branch] demo -> origin/demo
d80f8d7..359eab0 master -> origin/master
$ git checkout demo
Branch demo set up to track remote branch demo from origin.
Switched to a new branch 'demo'
As you can see, running git fetch origin
retrieved any remote branches we were not yet setup to track on our local machine. From there, since we now have a ref to the remote branch, we can simply run git checkout remote_branch
and we’ll gain the benefits of remote tracking.
I tried the above solution, but it didn’t work. Try this, it works:
git fetch origin 'remote_branch':'local_branch_name'
This will fetch the remote branch and create a new local branch (if not exists already) with name local_branch_name
and track the remote one in it.
To clone a Git repository, do:
git clone <either ssh url /http url>
The above command checks out all of the branches, but only the master
branch will be initialized. If you want to checkout the other branches, do:
git checkout -t origin/future_branch (for example)
This command checks out the remote branch, and your local branch name will be same as the remote branch.
If you want to override your local branch name on checkout:
git checkout -t -b enhancement origin/future_branch
Now your local branch name is enhancement
, but your remote branch name is future_branch
.