track - git switch branch
How do I check out a remote Git branch? (18)
Somebody pushed a branch called
git push origin test to a shared repository. I can see the branch with
git branch -r.
Now I'm trying to check out the remote
git checkout testwhich does nothing
git checkout origin/testgives
* (no branch). Which is confusing. How can I be on "no branch"?
How do I check out a remote Git branch?
Accepted answer not working for you?
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.
Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, with only one remote, you can just do:
git fetch git checkout test
As user masukomi points out in a comment,
git checkout test will NOT work in modern git if you have multiple remotes. In this case use
git checkout -b test <name of remote>/test
Before you can start working locally on a remote branch, you need to fetch it as called out in answers below.
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
git fetch && git checkout your-branch-name
Sidenote: With modern Git (>= 1.6.6), you are able to use just
git checkout test
(note that it is 'test' not 'origin/test') to perform magical DWIM-mery and create local branch 'test' for you, for which upstream would be remote-tracking branch 'origin/test'.
* (no branch) in
git branch output means that you are on unnamed branch, in so called "detached HEAD" state (HEAD points directly to commit, and is not symbolic reference to some local branch). If you made some commits on this unnamed branch, you can always create local branch off current commit:
git checkout -b test HEAD
To get newly created branches
To switch into another branch
git checkout BranchName
git fetch --all git checkout -b <ur_new_local_branch_name> origin/<Remote_Branch_Name>
are equal to
git fetch --all
git checkout -b fixes_for_dev origin/development
Both will create a
latest fixes_for_dev from
First, you need to do:
git fetch # If you don't know about branch name
git fetch origin branch_name
Second, you can check out remote branch into your local by:
git checkout -b branch_name origin/branch_name
-b will create new branch in specified name from your selected remote branch.
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.
I was stuck in a situation seeing
error: pathspec 'desired-branch' did not match any file(s) known to git. for all of the suggestions above. I'm on git version 126.96.36.199.
So this worked for me:
git fetch origin desired-branch git checkout -b desired-branch FETCH_HEAD
The explanation behind is that I've noticed that when fetching the remote branch, it was fetched to FETCH_HEAD:
$ git fetch origin desired-branch From github.com:MYTEAM/my-repo * branch desired-branch -> FETCH_HEAD
If the branch is on something other than the
origin remote I like to do the following:
$ git fetch $ git checkout -b second/next upstream/next
This will checkout the
next branch on the
upstream remote in to a local branch called
second/next. Which means if you already have a local branch named next it will not conflict.
$ git branch -a * second/next remotes/origin/next remotes/upstream/next
In this case, you probably want to create a local
test branch which is tracking the remote
$ git branch test origin/test
In earlier versions of
git, you needed an explicit
--track option, but that is the default now when you are branching off a remote branch.
Other guys and gals give the solutions, but maybe I can tell you why.
git checkout test which does nothing
Does nothing doesn't equal
doesn't work, so I guess when you type 'git checkout test' in your terminal and press enter key, no message appears and no error occurs. Am I right?
If the answer is 'yes', I can tell you the cause.
The cause is that there is a file (or folder) named 'test' in your work tree.
git checkout xxx parsed,
- Git looks on
xxxas a branch name at first, but there isn't any branch named test.
- Then Git thinks
xxxis a path, and fortunately (or unfortunately), there is a file named test. So
git checkout xxxmeans discard any modification in
- If there isn't file named
xxxeither, then Git will try to create the
xxxaccording to some rules. One of the rules is create a branch named
git checkout with the name of the remote branch. Git will automatically create a local branch that tracks the remote one:
git fetch git checkout test
However, if that branch name is found in more than one remote, this won't work as Git doesn't know which to use. In that case you can use either:
git checkout --track origin/test
git checkout -b test origin/test
git remote show <origin name> command will list all branches (including un-tracked branches). Then you can find the remote branch name that you need to fetch.
$ git remote show origin
Use these steps to fetch remote branches:
git fetch <origin name> <remote branch name>:<local branch name> git checkout <local branch name > (local branch name should the name that you given fetching)
$ git fetch origin test:test $ git checkout test
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
git checkout -b <BRANCH-NAME> <REMOTE-NAME>/<BRANCH-NAME>
Other answers do not work with modern Git in my benign case. You might need to pull first if the remote branch is new, but I haven't checked that case.
You can try
git fetch remote git checkout --track -b local_branch_name origin/branch_name
git fetch git checkout -b local_branch_name origin/branch_name
none of these answers worked for me. this worked:
git checkout -b feature/branch remotes/origin/feature/branch