Skip to content

Instantly share code, notes, and snippets.

@ff6347
Last active April 13, 2023 19:34
Show Gist options
  • Save ff6347/4023446 to your computer and use it in GitHub Desktop.
Save ff6347/4023446 to your computer and use it in GitHub Desktop.
clone remote branch with git

This is taken from here. http://stackoverflow.com/questions/67699/how-do-i-clone-all-remote-branches-with-git/72156#72156 Want this as a gist because I always come back to this posting.

First, clone a remote git repository and cd into it:

$ git clone git://example.com/myproject
$ cd myproject

Next, look at the local branches in your repository:

$ git branch
* main

But there are other branches hiding in your repository! You can see these using the -a flag:

$ git branch -a
* main
  origin/HEAD
  origin/main
  origin/v1.0-stable
  origin/experimental

If you already have a existing repository but there is a new branch that is yet listed do a:

git fetch

If you just want to take a quick peek at an upstream branch, you can check it out directly:

$ git checkout origin/experimental

But if you want to work on that branch, you'll need to create a local tracking branch:

$ git checkout -b experimental origin/experimental

Now, if you look at your local branches, this is what you'll see:

$ git branch
  main
* experimental

You can actually track more than one remote repository using git remote.

$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* main
  origin/HEAD
  origin/main
  origin/v1.0-stable
  origin/experimental
  win32/main
  win32/new-widgets

At this point, things are getting pretty crazy, so run gitk to see what's going on:

$ gitk --all &
@teletrabajovenezuela1
Copy link

Thanks Fabian, Good Tutorial, really helped me with this of local and remote branches. Regards.

@JS-goose
Copy link

I realize this is 5+ years old but I stumbled across this while trying to figure out how to clone only a single repository. This helped me way more than any of the other answers I found. Thank you!

@spi-ros
Copy link

spi-ros commented Jul 31, 2018

Thank you very much!!!

@gebigoma
Copy link

Thanks for this!!!

@handsome-b-wonderful
Copy link

thanks - this was a good summary. I wanted to make a copy of my remote feature branch before force pushing a rebased local version so

$ git checkout -b security-blanket origin/feature-i-already-have-locally

gave me a little extra security to make sure I didn't mess anything up during my merge resolutions.

@vikranthkasi
Copy link

Thanks for this!

@ibraheem-nadeem
Copy link

Thanks! Very helpful

@ff6347
Copy link
Author

ff6347 commented Jun 28, 2019

Nice. I did not know that there where comments on this gist. Seems like GitHub changed something on the notification options…

You are all welcome.

@ramdandamudi
Copy link

This is helpful, Thank you.

@respici0
Copy link

respici0 commented Oct 26, 2019

Something to note.. for git checkout -b experimental origin/experimental, you can use git checkout --track origin/experimental. Here is a link to a good explanation of what the difference between both are:
https://stackoverflow.com/questions/10002239/difference-between-git-checkout-track-origin-branch-and-git-checkout-b-branch

@aloifran
Copy link

Thank you! This was really good explained and helped me a lot : )

@rkiwanuka
Copy link

Thank you!

@dagolinuxoid
Copy link

git checkout remote_branch_name

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