7587

Somebody pushed a branch called test with 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 test branch.

I've tried:

  • git checkout test which does nothing

  • git checkout origin/test gives * (no branch). Which is confusing. How can I be on "no branch"?

How do I check out a remote Git branch?

3
  • 28
    I think this thread is unhelpful. Nothing seems to work, the original question seems to have been lost in many of the answers. I have read every word, tried everything below, and have no idea how to do what the OP wants to do.
    – Tony Ennis
    Aug 26 '14 at 0:16
  • 31
    I feel like I'm taking crazy pills. I'm trying to checkout a branch from an upstream, not just origin, and every recommended answer doesn't do anything remotely helpful (pun-intended). EDIT - excuse me, the multitude of suggestions contained in the top 2 answers were useless; 3rd one (git branch test origin/test) is what works. Glad the top 2 have 20x the number of votes...
    – dwanderson
    Mar 9 '17 at 16:35
  • Maybe useful to someone else: When I used the Atom editor UI to fetch and pull changes, it pulled changes on the "main" branch but did not create a local reference to the second remote branch. Using git fetch on the command line created that reference, then I was able to checkout the branch as per several answers.
    – Scott Leis
    Nov 15 at 5:19

38 Answers 38

10378

With One Remote

Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, with only one remote, you can 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

or the shorthand

git checkout -t <name of remote>/test

With >1 Remotes

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

I also created the image below for you to share the differences, look at how to fetch works, and also how it's different to pull:

enter image description here

8
  • 442
    To expand on this: git doesn't allow you to work on someone else's branches. You can only work on your own. So if you want to add to someone else's branch, you need to create your own "copy" of that branch, which is what the above command does (well, it creates your branch and checks it out, too). Nov 23 '09 at 15:24
  • 148
    If it's a new remote branch you may need to git fetch before doing this so that git is aware of origin/test Nov 4 '11 at 14:38
  • 62
    ...and you would do this with git fetch origin test
    – Andrew
    Jan 22 '12 at 23:24
  • 23
    Error: "git checkout: updating paths is incompatible with switching branches. Did you intend to checkout origin/test which can not be resolved as commit?"
    – Xeoncross
    Sep 11 '12 at 20:35
  • 92
    git checkout test will NOT work in modern git if you have multiple remotes which have the same branch name. It can't know which one to use.
    – masukomi
    Sep 16 '14 at 15:34
1368

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'.


The * (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

** EDIT (by editor not author) **

I found a comment buried below which seems to modernize this answer:

@Dennis: git checkout <non-branch>, for example git checkout origin/test results in detached HEAD / unnamed branch, while git checkout test or git checkout -b test origin/test results in local branch test (with remote-tracking branch origin/test as upstream) – Jakub Narębski Jan 9 '14 at 8:17

emphasis on git checkout origin/test

5
  • 36
    Unsurprising, but this version has been released in the last few years - knowing this syntax can save a lot of time since there's still a lot of old documentation and comment threads floating around that suggest the older method for doing this.
    – Curtis
    Apr 16 '12 at 13:24
  • 12
    "modern git"--for the record, (approx) what version are you referring to? Sometimes we have to work on systems running older distros. Aug 28 '12 at 2:30
  • 13
    @aidan If you get a response like error: pathspec 'branch_name' did not match any file(s) known to git. then you should do a git fetch first.
    – Dennis
    Oct 18 '13 at 0:40
  • 6
    Using git version 1.8.3.msysgit.0 and this doesn't work for me - did not match any file(s) known to git - I've done many git fetches
    – PandaWood
    Dec 3 '13 at 23:59
  • 10
    @Dennis: git checkout <non-branch>, for example git checkout origin/test results in detached HEAD / unnamed branch, while git checkout test or git checkout -b test origin/test results in local branch test (with remote-tracking branch origin/test as upstream) Jan 9 '14 at 8:17
618

In this case, you probably want to create a local test branch which is tracking the remote test branch:

$ 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.

1
  • 19
    This will create a local branch without switching to it. Oct 16 '13 at 7:20
517

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?

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.

0
318

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.

6
  • 41
    This worked for me when neither git fetch origin or git remote update created local branches. I'm not sure why.
    – Godsmith
    Sep 11 '14 at 8:45
  • 7
    This was the most direct way to accomplish what I needed which was to use a remote branch (not master) to create a new branch.
    – Roralee
    Nov 13 '15 at 23:15
  • 8
    Worked seamlessly, especially when having cloned a single branch from a remote with multiple branches.
    – Alex C
    Oct 17 '16 at 10:19
  • 9
    this worked for me too, where accepted answers and other high voted didn't. My git version is 2.5.0
    – pdepmcp
    Feb 17 '17 at 12:46
  • 7
    Does anyone have any idea why this works when everything else doesn't? (I'm on git 2.13.0) Jun 19 '17 at 18:43
114

This will DWIM for a remote not named origin (documentation):

$ git checkout -t remote_name/remote_branch

To add a new remote, you will need to do the following first:

$ git remote add remote_name location_of_remote
$ git fetch remote_name

The first tells Git the remote exists, the second gets the commits.

0
111

Use:

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.

1
  • 15
    Looking at it now, they do overlap. Only mine is succinct and tells you what to do rather than tell a story. I assume it can be more useful therefore, especially for nowadays git versions. You can downvote it if you think it is a bad answer.
    – matanster
    Feb 21 '16 at 11:34
109

You basically see the branch, but you don't have a local copy yet!...

You need to fetch the branch...

You can simply fetch and then checkout to the branch, use the one line command below to do that:

git fetch && git checkout test

I also created the image below for you to share the differences, look at how fetch works and also how it's different to pull:

git fetch

0
70

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.

0
45

You can try

git fetch remote
git checkout --track -b local_branch_name origin/branch_name

or

git fetch
git checkout -b local_branch_name origin/branch_name
2
  • 4
    FYI, --track is no longer needed in newer versions of git, because it's set by default, as explained in this earlier answer.
    – user456814
    Jun 21 '14 at 18:03
  • This comment worked for me, thank you! git checkout -b local_branch_name origin/branch_name
    – Tim Kelly
    Sep 15 at 23:39
37

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.

0
32

I use the following command:

git checkout --track origin/other_remote_branch
1
  • 15
    This answer would be a lot more useful if you explain why you are using it this way. i.e. why someone should use '--track' and so on... Nov 20 '17 at 3:19
28

Commands

git fetch --all
git checkout -b <ur_new_local_branch_name> origin/<Remote_Branch_Name>

are equal to

 git fetch --all

and then

 git checkout -b fixes_for_dev origin/development

Both will create a latest fixes_for_dev from development

27

Simply run 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

or

git checkout -b test origin/test

In 2.19, Git learned the checkout.defaultRemote configuration, which specifies a remote to default to when resolving such an ambiguity.

25

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
25

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 1.8.3.1.

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
23

none of these answers worked for me. this worked:

git checkout -b feature/branch remotes/origin/feature/branch

0
21

git fetch && git checkout your-branch-name

20

The 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.

Example:

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

Example:

$ git fetch origin test:test
$ git checkout test
1
  • Good lord, thank you. Been using git for a decade but was on a giant repo drowning all the sudden, just trying to check out a branch... This got me going!
    – swajak
    Sep 30 at 14:22
17

git branch -r says the object name is invalid, because that branch name isn't in Git's local branch list. Update your local branch list from origin with:

git remote update

And then try checking out your remote branch again.

This worked for me.

I believe git fetch pulls in all remote branches, which is not what the original poster wanted.

1
  • 2
    FYI, git remote update will also fetch all remote branches.
    – user456814
    Jun 21 '14 at 17:59
15

Fetch from the remote and checkout the branch.

git fetch <remote_name> && git checkout <branch_name> 

E.g.:

git fetch origin && git checkout feature/XYZ-1234-Add-alerts

0
13

git checkout -b "Branch_name" [ B means Create local branch]

git branch --all

git checkout -b "Your Branch name"

git branch

git pull origin "Your Branch name"

successfully checkout from the master branch to dev branch

enter image description here

0
12

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.

When git checkout xxx parsed,

  1. Git looks on xxx as a branch name at first, but there isn't any branch named test.
  2. Then Git thinks xxx is a path, and fortunately (or unfortunately), there is a file named test. So git checkout xxx means discard any modification in xxx file.
  3. If there isn't file named xxx either, then Git will try to create the xxx according to some rules. One of the rules is create a branch named xxx if remotes/origin/xxx exists.
0
12

To get newly created branches

git fetch

To switch into another branch

git checkout BranchName
8

You can start tracking all remote branches with the following Bash script:

#!/bin/bash
git fetch --all
for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`
  do git branch -f --track "$branch" "origin/$branch"
done

Here is also a single-line version:

git fetch --all; for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`; do git branch --track "$branch" "origin/$branch" ; done ;
8

to get all remote branches use this :

git fetch --all

then checkout to the branch :

git checkout test
0
8

I always do: git fetch origin && git checkout --track origin/branch_name

0
7

Just run these two commands and you should be good to go.

git checkout <branch-name>
git pull <remote> <branch-name>
0
6

For us, it seems the remote.origin.fetch configuration gave a problem. Therefore, we could not see any other remote branches than master, so git fetch [--all] did not help. Neither git checkout mybranch nor git checkout -b mybranch --track origin/mybranch did work, although it certainly was at remote.

The previous configuration only allowed master to be fetched:

$ git config --list | grep fetch
remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master

Fix it by using * and fetch the new information from origin:

$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

$ git fetch
...
 * [new branch] ...
...

Now we could git checkout the remote branch locally.

No idea how this config ended up in our local repo.

0
6

If the remote branch name begins with special characteres you need to use single quotes around it in the checkout command, or else git won't know which branch you are talking about.

For example, I tried to checkout a remote branch named as #9773 but the command didn't work properly, as shown in the picture below:

enter image description here

For some reason I wondered if the sharp symbol (#) could have something to do with it, and then I tried surrounding the branch name with single quotes, like '#9773' rathen than just #9773, and fortunately it worked fine.

$ git checkout -b '#9773' origin/'#9773'
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.