Skip to content

Instantly share code, notes, and snippets.

@markSci5
Created July 3, 2013 07:08
Show Gist options
  • Save markSci5/5916003 to your computer and use it in GitHub Desktop.
Save markSci5/5916003 to your computer and use it in GitHub Desktop.
Git checkout remote branch
//To fetch a branch, you simply need to:
git fetch origin
//This will fetch all of the remote branches for you. 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
//Or
git branch test origin/test
@2016IsTeamWork
Copy link

Thanks!

@subfuzion
Copy link

subfuzion commented Sep 1, 2016

With newer versions of git you can just enter:

$ git fetch
$ git checkout <branch>

git fetch will fetch all the remote branches, which you can verify with git branch -r (or git branch -rv), and as long as you don't have an existing branch with the name you want, you can just switch directly to it with git checkout <branch>. All this behavior assumes the default configuration for fetching "refs" for all remote branches and tags, which you can override with options or by configuring the remote. See git fetch --help for details. I like to also include the -p (--prune) option for removing dead remote-tracking refs for refs that have since been deleted on the remote.

@amarjitfantain
Copy link

Thanks

@rajinder-yadav
Copy link

Exactly what I was looking for sweet!

@zbobyuan
Copy link

zbobyuan commented Dec 3, 2016

awesome!

@americanhanko
Copy link

In the rare case that you have a folder at the root of the repository with the same name (test/) as the remote branch (origin/test), you won't be able to checkout to the remote branch using git checkout <branch>. In this case, you'd need to run git checkout --track origin/test (which is the same thing as running git checkout -b test origin/test)

@mam8
Copy link

mam8 commented Apr 19, 2017

@subfuzion Good points! Thanks.

@monkindey
Copy link

@subfuzion Thanks for the sharing, Learn a lot from it.

@bardware
Copy link

which is the same thing as running git checkout -b test origin/test

Where does my data end up when I call git push? Should I set some upstream?

@cameronove
Copy link

@americanhanko: 'folder at the root of the repository with sam name' - you saved me dude - thanks a lot!!!!

@kambleaa007
Copy link

which is the same thing as running git checkout -b test origin/test

Where does my data end up when I call git push? Should I set some upstream?

How to do git push .......

@chaufon
Copy link

chaufon commented Aug 3, 2020

Thanks for this tip!

@markstrefford
Copy link

Don't forget to do a git pull first otherwise git checkout branch will fail with unknown path spec.

@developerbello
Copy link

Thanks very much

@chikosan
Copy link

git fetch origin name:name (will download a specific branch)

@AlphaTechnolog
Copy link

git fetch origin name:name (will download a specific branch)

THANK YOU SO MUCH, literally, this is the only solution that worked to me!! :>

@alfredomp
Copy link

To check remote branches use: git branch -r

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment