How do I check out a remote Git branch?

Multi tool use
Multi tool use


How do I check out a remote Git branch?



Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r.


test


git push origin test


git branch -r



Now I'm trying to check out the remote test branch.


test



I've tried:



git checkout test which does nothing


git checkout test



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


git checkout origin/test


* (no branch)



How do I check out a remote Git branch?





@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)
– fachexot
Feb 1 '14 at 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.
– Tony Ennis
Aug 26 '14 at 0:16





Git commands are not intuitive to begin with, add the changes introduced with recent versions to the mix and you have this page...
– Christophe Roussy
Jan 12 '16 at 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...
– dwanderson
Mar 9 '17 at 16:35



upstream


origin


git branch test origin/test





Maybe you have a file named 'test' in your work tree, see stackoverflow.com/a/45006389/792416 for detail.
– oldman
Jul 10 '17 at 7:50




23 Answers
23



Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, you can just do:


git fetch
git checkout test



(User masukomi points out below that 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).


git checkout test


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





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).
– Dan Moulding
Nov 23 '09 at 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
– Neil Sarkar
Nov 4 '11 at 14:38



git fetch


origin/test





...and you would do this with git fetch origin test
– Andrew
Jan 22 '12 at 23:24


git fetch origin test





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



origin/test





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


git checkout test



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:


* (no branch)


git branch


git checkout -b test HEAD





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





"modern git"--for the record, (approx) what version are you referring to? Sometimes we have to work on systems running older distros.
– Craig McQueen
Aug 28 '12 at 2:30






"modern git" in this context is git 1.6.6
– Bobby Norton
Mar 19 '13 at 20:29






@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


error: pathspec 'branch_name' did not match any file(s) known to git.





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



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


test


test


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


git


--track





This will create a local branch without switching to it.
– Alex.Designworks
Oct 16 '13 at 7: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
– PandaWood
Dec 4 '13 at 0: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.
– pal4life
Jun 18 '14 at 20:01





git branch test works for me
– Isaac Pak
Feb 17 '17 at 15:27



git branch test



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


origin


git checkout remote_branch


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


git fetch origin


git checkout remote_branch





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.
– elliotrock
Feb 2 '15 at 5:51





git checkout -b newbranch also works great for 1-step create and checkout a new branch based on the current branch.
– Linus
May 19 '16 at 13:41


git checkout -b newbranch





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.
– delicateLatticeworkFever
May 26 '16 at 12:51



git ls-remote


git checkout -b [branch] --track [remote/branch]


git pull [remote] [branch]





and when this answer doesn't work either, see this one.
– eis
Jan 16 at 12:48



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.


local_branch_name





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





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





Worked seamlessly, especially when having cloned a single branch from a remote with multiple branches.
– Alex C
Oct 17 '16 at 10:19





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





Does anyone have any idea why this works when everything else doesn't? (I'm on git 2.13.0)
– Nathan Arthur
Jun 19 '17 at 18:43




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.



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.





Do you realize that this is an extract of this answer
– Thomas Ayoub
Feb 21 '16 at 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.
– matanster
Feb 21 '16 at 11:34




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:


master


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.


enhancement


future_branch



Documentation





git clone <either ssh url /http url> - works perfectly for me
– Kmeixner
Dec 16 '14 at 16:56





Yes you are correct. Thanks for your information, I will update it very soon @warvariuc
– Madhan Ayyasamy
Jan 8 '15 at 10:45





If the remote has no master, this is not going to work.
– polkovnikov.ph
Sep 27 '16 at 16:47


master



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


fetch



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:


fetch


pull



git fetch





Great picture, thanks
– developer
Sep 8 '17 at 10:05





Does not work if there is no local branch test
– Dmitri Zaitsev
Apr 25 at 3:26


test





@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...
– Alireza
Apr 25 at 5: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.
– Dmitri Zaitsev
Apr 25 at 12:51


test


git pull





@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...
– Alireza
Apr 25 at 13:36



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





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


--track



First, you need to do:



git fetch # If you don't know about branch name


git fetch


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.


-b





I dont understand -b. If you can do "git checkout master" why cant you do "git checkout origin/test"?
– John Little
Jan 23 at 18:16





-b for new branch which is from origin/master
– Mohideen ibn Mohammed
Jan 24 at 1:13



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


latest fixes_for_dev


development



If the branch is on something other than the origin remote I like to do the following:


origin


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


next


upstream


second/next


$ git branch -a
* second/next
remotes/origin/next
remotes/upstream/next



I use the following command:


git checkout --track origin/other_remote_branch





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...
– Matt Friedman
Nov 20 '17 at 3:19



git fetch && git checkout your-branch-name


git fetch && git checkout your-branch-name



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 branch -r


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.


git fetch





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



git remote update



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.


git remote show <origin name>



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





@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.
– Qh0stM4N
Jan 30 at 13:52


$ git clone -b release --single-branch --depth 5 https://github.com/user/repo.git


$ git remote show origin



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 ;



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?


Does nothing


doesn't work



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,


git checkout xxx


xxx


xxx


git checkout xxx


xxx


xxx


xxx


xxx


remotes/origin/xxx



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'



Fetch origin and checkout the branch.


git fetch origin && git checkout branch_name



To get newly created branches


git fetch



To switch into another branch


git checkout BranchName



none of these answers worked for me. this worked:



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


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




Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).


Would you like to answer one of these unanswered questions instead?

Y Ft,4tRl7v ywdk i329sFFh3
Tnpr2qWNTsH8IpJ3tkhvWa0xbugORtB3 mlz on0PWKW72H

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications