Somebody pushed a branch called test
with git push origin test
to a shared repository. I can see the branch with git branch -r
.
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?
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.