11

If I do

$ git clone https://github.com/dtu-compute/docker-mongodb
$ cd docker-mongodb
$ git branch
* master

but if you look at the Github branch page, there are two branches.

Question

Why don't I get all the branches with git clone?

Tim
  • 41,901
  • 18
  • 127
  • 145
Jasmine Lognnes
  • 6,597
  • 9
  • 38
  • 58
  • 5
    if you checkout the branch you will see it works fine – Tim Jun 09 '16 at 15:02
  • 1
    But why isn't it listed? have it something to do how the branch were pushed? – Jasmine Lognnes Jun 09 '16 at 15:04
  • 7
    `git branch -a` will list all of them – Tim Jun 09 '16 at 15:04
  • 2
    If a remote repo has no master branch, you may ask why `git clone` clones nothing. In fact, it clones everything if no option like --single-branch or --depth is used. They are hidden at first. `git branch` only lists the local branches. – ElpieKay Jun 09 '16 at 15:11

1 Answers1

14

git branch only shows local branches by default; use git branch -r to see remote branches or git branch -a to see all. git clone only creates one local branch, master by default, which tracks (again, by default) the master branch on the remote repository.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • But then what about when you want to work on one of those branches that exist remotely but not locally? If I run `git checkout exists-remotely` it says it's created a new branch, which presumably branches off master. How would you ensure you're getting the existing, *remote* `exists-remotely` rather than making a new one? – Mitya May 04 '22 at 18:15
  • 1
    You presume wrong. "If is not found but there does exist a tracking branch in exactly one remote (call it ) with a matching name and --no-guess is not specified, treat as equivalent to $ git checkout -b --track /" – chepner May 04 '22 at 18:17
  • OK so when I clone a repo, and I end up with just master, and I then checkout a branch that isn't listed locally but I know exists remotely, why then does it tell me it's switched "to a new branch" and the files/code differ from the remote branch of the same name? Struggling to get my head around this. – Mitya May 04 '22 at 18:19
  • 2
    Because it creates a new *tracking* branch that mirrors the remote, not a new branch based on your currently checked out branch, before switching. – chepner May 04 '22 at 18:37
  • Thanks, that clears that up. Appreciated. – Mitya May 05 '22 at 11:10