How do I check out a remote Git branch?

Created 23.11.2009 14:23
Viewed 5.85M times
7279 votes

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?

9
Comments
@inger But it does not include the possibility to rename the new local branch (if you want to --set-upstream later on and keep naming consistency) by fachexot, 01.02.2014 12:43
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. by Tony Ennis, 26.08.2014 00:16
Git commands are not intuitive to begin with, add the changes introduced with recent versions to the mix and you have this page... by Christophe Roussy, 12.01.2016 17:41
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... by dwanderson, 09.03.2017 16:35
Maybe you have a file named 'test' in your work tree, see stackoverflow.com/a/45006389/792416 for detail. by oldman, 10.07.2017 07:50
Here's what worked for me in 2017. Two steps: 1) git fetch origin <branchname> 2) git checkout -b <branchname> origin/<branchname> by user3089840, 03.08.2017 18:42
git checkout --track origin/test by eigenfield, 25.10.2018 01:37
tldr; The syntax is like git checkout -b <new-branch> <existing-branch>. by Honey, 15.04.2019 21:17
We now have a new command to checkout branches, its the switch. git swtich <branchName> by Humble Bee, 21.04.2021 07:41
Show remaining 4 comments
Answers 35
32
9903

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
23.11.2009 14:26
Comments
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). by Dan Moulding, 23.11.2009 15:24
If it's a new remote branch you may need to git fetch before doing this so that git is aware of origin/test by Neil Sarkar, 04.11.2011 14:38
...and you would do this with git fetch origin test by Andrew, 22.01.2012 23:24
Error: "git checkout: updating paths is incompatible with switching branches. Did you intend to checkout origin/test which can not be resolved as commit?" by Xeoncross, 11.09.2012 20:35
@Xeoncross Read this answer how to fix it by Gramic, 02.10.2012 09:32
I was getting the same error, and I just had to do a git fetch first: Git checkout on a remote branch does not work by mltsy, 20.11.2012 20:51
In case this helps anyone- for this to work for me, I had to run git pull origin first by mikermcneil, 05.04.2013 01:04
fetch first: git fetch --all by orluke, 31.05.2013 01:12
As pointed out by Jakub - you should be able to use a shorter and safer checkout command since 1.6.6. Please consider selecting his answer. by inger, 24.07.2013 09:50
This doesn't define "fetch" other than to say that you can fetch with the fetch command. An obvious new user might appreciate a word or two about what is being gotten from where to where. by Lee Meador, 19.09.2013 17:05
Worked for me, but after executing git remote update before git checkout -b test origin/test by tabdulradi, 20.11.2013 10:53
@AdriánSalgado the -b causes a new branch to be created as if you had written "git branch test origin/test; git checkout test" by hallski, 26.01.2014 23:24
@jordan-dea-mattson I'm not confident enough to edit the answer but I just tried this shorter version and it works: git fetch; git checkout test. It automatically sets up the correct remote tracking branch. by Andy Baker, 21.02.2014 09:39
Where does git get the URL info from? Will I end up checking out a branch inside the current workspace? by Nick, 07.07.2014 23:51
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. by masukomi, 16.09.2014 15:34
The previous comment seems like it needs to be a lot more prominent - can someone add this to the answer? by kcrisman, 30.10.2014 15:34
Sorry to tell you, this doesnt work. it will create a local branch that has no idea that the remote branch is related to it by FRR, 05.02.2015 15:11
I had multiple remotes where the branch only existing in one, yet it still didn't work. So I've updated the answer to be less specific. by samthebest, 04.03.2015 16:27
"git checkout test will NOT work in modern git if you have multiple remotes" -- I have a single remote and I still need to do git checkout -b test origin/test... by Berislav Lopac, 17.11.2015 09:33
yeah, but after making changes, then what? by botbot, 26.12.2015 06:03
And do not forget to pull the latest changes git pull by Todor Todorov, 25.01.2016 12:44
Do not include origin/ at the front of my branch spec when checking it out by Alok Adhao, 17.05.2016 11:17
@BerislavLopac and @samethebest the git checkout test was not working for me either with multiple remotes but only one branch with the name test... but I finally realized my problem was that I had a folder named test. So git was just checking out that folder. Thus I still needed to do git checkout -b test origin/test by Ryan Walls, 27.06.2016 14:35
Hi @hallski, I want to know what's the purpose of git fetch origin? Thanks :) by KarenAnne, 30.08.2016 02:57
@KarenAnne It fetches the latest changes from the repository 'origin' (often your remote remote repository, for example on Github). by hallski, 21.09.2016 12:44
this is bad semantics on git's part "git checkout -b test origin/test" it should be this instead "git checkout -b origin/test test", oh well by Alexander Mills, 14.10.2016 23:52
"This will fetch all of the remote branches for you." is not a true statement. by AKWF, 20.12.2018 13:19
The syntax used in this answer is: git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>] which is basically: git checkout -b test origin/test. @hallski maybe you can include the syntax in the answer too by Honey, 13.04.2020 20:02
answer also knowed as: how to get rich with upvotes by Lucas Vazquez, 04.05.2020 19:05
I've also seen git fetch && git checkout <branchname> work by Alex Bishka, 23.06.2020 00:27
git has a newer command for this - git switch <branch> by Nitsan Avni, 05.08.2020 12:03
One more thing would be nice to elaborate, i.e. checking out the branch without adding a remote (let's say alien branch). git fetch $REMOTE_URL $BRANCH && git checkout -b $LOCAL_BRANCH FETCH_HEAD by 0andriy, 25.01.2021 18:02
Show remaining 27 comments
16
1332

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

24.11.2009 00:17
Comments
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. by Curtis, 16.04.2012 13:24
"modern git"--for the record, (approx) what version are you referring to? Sometimes we have to work on systems running older distros. by Craig McQueen, 28.08.2012 02:30
"modern git" in this context is git 1.6.6 by Bobby Norton, 19.03.2013 20:29
@knite: The git checkout test is more DWIM-my. Not only it adds --track option, but when checking out nonexistent branch it notices that there exist remote-tracking branch of the same name and automagically creates local branch for you that you can checkout (you cannot checkout remote-tracking branch, as it is controlled by outside remote repository). HTH by Jakub Narębski, 26.06.2013 07:06
I had pushed a branch to my repo and then modified the README via github. I then wanted to update my local copy. Using git checkout mybranch lead to git suggesting me to use git pull because mybranch already existed. Thanks for leading me towards the solution! by Meredith, 19.07.2013 22:17
I'm on git version 1.7.10.2 (Apple Git-33) and git checkout test didn't work for me. Had to fall back to the accepted answer. I probably have some strange config interfering somewhere. by aidan, 18.09.2013 00:55
See tcaswell's answer below if the remote of interest is not named origin by Jean Jordaan, 16.10.2013 09:03
@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. by Dennis, 18.10.2013 00:40
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 by PandaWood, 03.12.2013 23:59
@PandaWood Didn't work for me either, using 1.8.3.4 (Apple Git-47) by LasagnaAndroid, 08.01.2014 15:46
@PandaWood @AdriánSalgado In your repo, open .git/config and check that the refspec for fetching from your remote (e.g. origin) doesn't only fetch from a specific branch. For example, this is bad: fetch = +refs/heads/master:refs/remotes/origin/master because it only fetches master. It should look like fetch = +refs/heads/*:refs/remotes/origin/*. Then use git fetch origin after that. If successful, the branch you're interested in should appear in the fetch output, and it should also appear in the output of git branch -a as a remote branch. by Dennis, 09.01.2014 02:03
@JakubNarębski What is the relevance of the lower section of your answer about the unnamed branch? With git v1.8.3.2 I'm not on an unnamed branch after checking out (and tracking) the remote branch. by Dennis, 09.01.2014 02:14
@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) by Jakub Narębski, 09.01.2014 08:17
Using git checkout -B origin/branch will create and checkout the desired remote branch in one line. where as -b option will only create the branch and put HEAD in detached state. by IskandarG, 17.08.2017 09:34
git checkout origin/feature/my_branch give me You are in 'detached HEAD' state. on git version 2.20.1 (Apple Git-117) by mrgloom, 02.07.2019 10:37
@mrgloom: what about using git checkout feature/my_branch, without the origin/ prefix? by Jakub Narębski, 05.07.2019 15:28
Show remaining 11 comments
3
593

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.

23.11.2009 14:27
Comments
This will create a local branch without switching to it. by Alex Skrypnyk, 16.10.2013 07:20
Though I got fatal: Ambiguous object name: 'origin/dev' - where a branch 'dev' on origin most definitely exists - but I'd accidentally created a branch called "origin/dev" on my machine (in my previous stupid attempts to get this right, no doubt) ... ouch by PandaWood, 04.12.2013 00:04
This has been giving me the error error: failed to push some refs to hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. Check out this branch and merge the remote changes hint: (e.g. 'git pull') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. by pal4life, 18.06.2014 20:01
6
498

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.

07.12.2012 20:52
Comments
I'll add a note if you have a separate branch locally: Make sure you have associated this with the remote repo using 'git remote add origin [the_path_to_your_repo/repo_name.git]' . Then use 'git fetch origin' where 'origin' means the origin repository you have made the association with. by elliotrock, 02.02.2015 05:51
git checkout -b newbranch also works great for 1-step create and checkout a new branch based on the current branch. by Linus, 19.05.2016 13:41
I think this is the most up-to-date (it keeps $@#!ing changing!). Git 2.5.5 I found the only way to actually see the remote branches was git ls-remote and the only way to actually use one is git checkout -b [branch] --track [remote/branch]...and that's after git pull [remote] [branch] worked. I.e., it actually pulled the whole branch, but still wouldn't list it. by CodeClown42, 26.05.2016 12:51
and when this answer doesn't work either, see this one. by eis, 16.01.2018 12:48
just to add, if you git fetch other_remote only_branch, you still get the fatal error. You have to git fetch other_remote without the branch name. Weird design. by est, 29.12.2018 02:56
This worked for me! by Antonio Pavicevac-Ortiz, 22.04.2021 21:15
Show remaining 1 comments
19
294

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.

18.10.2013 05:55
Comments
This worked for me when neither git fetch origin or git remote update created local branches. I'm not sure why. by Godsmith, 11.09.2014 08:45
it depends on git version but that seems to be made possible with its latest release. by Aftab Naveed, 15.10.2015 23:02
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. by Roralee, 13.11.2015 23:15
Worked seamlessly, especially when having cloned a single branch from a remote with multiple branches. by Alex C, 17.10.2016 10:19
this worked for me too, where accepted answers and other high voted didn't. My git version is 2.5.0 by pdepmcp, 17.02.2017 12:46
I had a branch of that form: "xx/xx" and This was the solution for me. by User, 14.03.2017 08:50
Does anyone have any idea why this works when everything else doesn't? (I'm on git 2.13.0) by Nathan Arthur, 19.06.2017 18:43
Works as of 2017 where the git version is 2.7.4 by kisanme, 15.08.2017 06:37
This is the only answer that worked for me on mac OS Sierra, git version 2.4.0 by Nj Subedi, 01.10.2017 14:07
At this point I don't know if this is what OP was asking or whatever but what this command allows me to do is automatically pull down fast forward commits. So for instance, after a pull request to master you don't want to wait for your IDE to reload the whole solution. Right after the PR you can run this command, then switch to master and create a new branch for the next feature. THANKS FOR THIS! by Ryanman, 30.11.2017 19:38
If this is the only thing that works for you, might want to check if you have this issue. For me, it prevented anything else from working. by eis, 16.01.2018 14:26
This is the only answer that worked for me git version 2.15.1 by Ruan Carlos, 25.02.2018 07:25
I've gone through this like 50 times already... git fetch --all fetches the branches but does not create them. git checkout -b branchName checkouts a new branch based on your current branch. git checkout upstream/newBranch creates a branch literally called upstream/newBranch. This is the 100% correct answer. by damusix, 05.06.2018 17:56
This is the only way that worked for me on cloned repo with git clone --single-branch flag by Denis Pereverzev, 19.09.2018 11:51
Same experience as others here; this worked, higher voted answers did not. by Andrew Price, 12.07.2019 20:04
The users ending here did a checkout of a single branch. This leads to an entry in .git/config like fetch = +refs/heads/xybranch:refs/remotes/master/xybranch and thus fetch will only fetch that one branch. You could also hack your .git/config and replace that branch name with a * which reads fetch = +refs/heads/*:refs/remotes/master/*... so this solution is the clean way to track further branches and thus fetch and check these out. by bebbo, 12.02.2020 20:29
This worked for me while nothing else worked. I had cloned the remote repo as a single branch by git clone --single-branch --branch <branchname> <remote-repo> by DeepSpace101, 20.05.2020 18:28
this actually worked, while all the other hints didn't. I am on git version 2.17.1 by sal, 08.10.2020 08:49
Don't forget git checkout local_branch_name to switch to a new branch after you fetch a remote branch. by Serhii Popov, 04.01.2021 15:35
Show remaining 14 comments
4
110

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.

21.02.2016 10:58
Comments
Do you realize that this is an extract of this answer by Thomas Ayoub, 21.02.2016 11:03
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. by matanster, 21.02.2016 11:34
git fetch is the safe option. Try fetch before using pull. Both download remote content, but while fetch does not change local state, pull will immediately change the local state. by Peter Szalay, 03.04.2019 08:07
do not foget to do git pull afetr that ... by mishaikon, 16.02.2021 10:34
0
111

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.

27.04.2012 22:37
4
93

OK, the answer is easy... 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

05.09.2017 14:20
Comments
@DmitriZaitsev yes, it will work, if the remote branch is there, and you do fetch, you will get the branch locally... git fetch && git checkout test..So this works, unless there is no remote branch, but the question saying there is already a remote branch there... by Alireza, 25.04.2018 05:13
The way I see the question, test looks like a new branch, so it is not likely to be present locally. Otherwise you could pull it easier with single git pull command. by Dmitri Zaitsev, 25.04.2018 12:51
@DmitriZaitsev, yes, that's why I said git fetch, that check for remote branches which just created, pull can bring other unwanted stuffs in, but fetch makes all branches available if you already have the repo locally... by Alireza, 25.04.2018 13:36
Wouldn't fetch without pull leave changes in the fetched copy but not in the local branch, leading to your local branch not being up-to-date? by Dmitri Zaitsev, 25.04.2018 14:38
3
67

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.

21.01.2013 10:04
Comments
git clone <either ssh url /http url> - works perfectly for me by Kmeixner, 16.12.2014 16:56
Yes you are correct. Thanks for your information, I will update it very soon @warvariuc by Madhan Ayyasamy, 08.01.2015 10:45
If the remote has no master, this is not going to work. by polkovnikov.ph, 27.09.2016 16:47
1
44

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
24.03.2014 13:11
Comments
FYI, --track is no longer needed in newer versions of git, because it's set by default, as explained in this earlier answer. by User, 21.06.2014 18:03
2
36

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.

18.05.2017 13:55
Comments
I dont understand -b. If you can do "git checkout master" why cant you do "git checkout origin/test"? by John Little, 23.01.2018 18:16
-b for new branch which is from origin/master by Mohideen bin Mohammed, 24.01.2018 01:13
1
32

I use the following command:

git checkout --track origin/other_remote_branch
06.09.2017 14:41
Comments
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... by Matt Friedman, 20.11.2017 03:19
0
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

21.04.2016 19:10
0
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
01.03.2013 10:00
0
25

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.

13.09.2018 12:21
0
21

git fetch && git checkout your-branch-name

26.03.2014 07:00
1
23

none of these answers worked for me. this worked:

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

10.07.2018 02:11
Comments
thanks. I was wondering if I had to use the full path (remotes/origin/feature/branch) that I saw in git when calling git branch -a command, but I wasn't for sure, so I just used git checkout -b apps/FEATURE/branch origin/apps/FEATURE/branch and it appeared to work. Message: Branch 'apps/FEATURE/branch' set up to track remote branch 'apps/FEATURE/epicBranch' from 'origin'. Switched to a new branch 'apps/FEATURE/branch' by Chris22, 27.07.2018 15:41
0
22

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
10.10.2018 21:52
1
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.

06.06.2013 12:21
Comments
FYI, git remote update will also fetch all remote branches. by User, 21.06.2014 17:59
1
17

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
07.01.2018 13:12
Comments
@hallski answered not working 2.15.1 versions but i have reduce .git file weigth clone only 1 branch and filter history with --depth flag. for example $ git clone -b release --single-branch --depth 5 https://github.com/user/repo.git Wron't information $ git remote show originthis does not listed all remote branch with single branch cloned repositories. by Qh0stM4N, 30.01.2018 13:52
0
14

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

21.05.2018 11:33
0
12

To get newly created branches

git fetch

To switch into another branch

git checkout BranchName
15.03.2018 08:49
2
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.
10.07.2017 07:45
Comments
thanks, kept trying to figure out why git wasn't doing anything. by Mike R, 26.11.2018 19:05
Thanks, for the Does nothing != Doesn't work hint and the fallback description. Quick Solution: Use git checkout xxx -- to use step 3 right away. Git uses the -- to differ between branch and files, if no filename is given in the command it will skip step 2. by pixelbrackets, 16.10.2020 08:37
1
11

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

10.12.2019 06:29
Comments
git checkout -b "Branch_name" [ B means Create local branch] by Keshav Gera, 16.01.2020 10:28
0
8

to get all remote branches use this :

git fetch --all

then checkout to the branch :

git checkout test
23.09.2019 06:46
0
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 ;
11.08.2017 12:06
5
4

Use fetch to pull all your remote

   git fetch --all

To list remote branches:

   git branch -r

For list all your branches

   git branch -l
   >>outpots like-
     * develop
       test
       master

To checkout/change a branch

   git checkout master
24.05.2019 08:51
Comments
Tried git branch -l and no remote branch was shown. by Dmitri Zaitsev, 04.06.2019 16:36
use git pull --all or git fetch --all by Nasir Khan, 07.06.2019 16:17
Yes, and after that, git branch -l still shows only local branches. It seems to be working exactly as git branch without -l, so what is the reason for -l? by Dmitri Zaitsev, 08.06.2019 01:13
Also your answer does not answer the question - master is not a remote branch. by Dmitri Zaitsev, 08.06.2019 01:15
-l flag for list. You can use --list too by Nasir Khan, 09.06.2019 02:42
0
3

Please follow the command to create an empty folder. Enter that and use this command:

saifurs-Mini:YO-iOS saifurrahman$ git clone your_project_url
Cloning into 'iPhoneV1'...
remote: Counting objects: 34230, done.
remote: Compressing objects: 100% (24028/24028), done.
remote: Total 34230 (delta 22212), reused 15340 (delta 9324)
Receiving objects: 100% (34230/34230), 202.53 MiB | 294.00 KiB/s, done.
Resolving deltas: 100% (22212/22212), done.
Checking connectivity... done.
saifurs-Mini:YO-iOS saifurrahman$ cd iPhoneV1/
saifurs-Mini:iPhoneV1 saifurrahman$ git checkout 1_4_0_content_discovery
Branch 1_4_0_content_discovery set up to track remote branch 1_4_0_content_discovery from origin.
Switched to a new branch '1_4_0_content_discovery'
01.01.2016 10:31
1
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'
07.11.2018 11:17
Comments
In many unix shells the # character is used for comments so anything after the # will be ignored. This is a shell thing and not something specific to git. Using quotes of a backslash before the # should be enough. by Paulo Scardine, 07.11.2018 12:12
2
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.

20.09.2019 10:20
Comments
This solved it for me since I cloned a single branch by Pontus Holmbom, 19.03.2020 13:14
Yes, exactly the scenario for when I cloned using --single-branch and wanted to switch onto another by Slartibartfast, 10.02.2021 14:02
0
5

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

git checkout <branch-name>
git pull origin <branch-name>
26.06.2020 05:15
0
5

I used that one:

git fetch origin
git reset --hard origin/{branchname}
19.08.2020 07:28
1
5

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

13.01.2021 11:59
Comments
This works for me and is simple. by ennth, 20.02.2021 21:38
0
2

There are many alternatives, for example:

  • Alternative 1:

    git fetch && git checkout test
    

    It's the most simple way.

  • Alternative 2:

    git fetch
    git checkout test
    

    It's the same but in two steeps.

30.10.2019 10:56
0
2

You can add a new branch test on local and then use:

git branch --set-upstream-to=origin/test test
19.09.2020 02:57