limp_chimp
limp_chimp

Reputation: 15153

Check out a branch that exists in a remote

I'm using git version 1.8.3.1 (centos 7). I have a repository on github with a bunch of branches. I clone that repository and attempt to check out one of those branches, but it won't let me:

$ git clone my_repo
$ cd my_repo
$ git checkout my_desired_branch
error: pathspec 'my_desired_branch' did not match any file(s) known to git.

I know that this branch exists in the remote branch; I pushed it from another computer, and I can check it out on github no problem. Reading up on similar issues on stack overflow, the answers seem to variously consist of:

$ git fetch [--all]
$ git pull [--all]

However, none of these things solve my problem. Furthermore, no matter what I do, nothing except the master branch shows up when I list my branches:

$ git branch
* master
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git branch -r
  origin/HEAD -> origin/master
  origin/master

I also can't fetch my branch from the repo by hash:

$ git fetch origin 6175e3ae4c88669af8aa5cd12b

This command gives no output, even with --verbose... but inspecting the exit code shows that it's 1. And of course:

$ git checkout 6175e3ae4c88669af8aa5cd12b
fatal: reference is not a tree: 6175e3ae4c88669af8aa5cd12b

This is really frustrating. At this point I've tried seemingly every possible combination of fetch, pull, and branch, and no matter what I do, I can't get my local clone of this repo to even show that these branches exist, let alone let me check them out.

What is happening here? It seems like the simplest thing in the world: given that my repo has some branch that I want, clone the repo and check out that branch.

Upvotes: 5

Views: 6150

Answers (3)

kenorb
kenorb

Reputation: 166319

To make sure you fetch all the branches from the remote, try:

git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
git fetch --all

See: How to fetch all remote branches?

Upvotes: 5

michas
michas

Reputation: 26495

To examine a remote repository you can use git ls-remote:

$ git ls-remote https://github.com/git/git.git
ff4ea6004fb48146330d663d64a71e7774f059f9        HEAD
a08595f76159b09d57553e37a5123f1091bb13e7        refs/heads/maint
ff4ea6004fb48146330d663d64a71e7774f059f9        refs/heads/master
b2aa7f843ccc79397d54723df13034a8761f5a18        refs/heads/next
0fea0509a8d85f1133db93c39b00e2c6ef285b4f        refs/heads/pu
[...]

This will list all the refs (branches/tags/etc) present at that remote repository and the hash they point to. After you clone that repository you should be able to access all those refs.

Are you sure you cloned using the correct address? What does git ls-remote list in your case? If your repository is public, please give the full address you used for cloning.

Upvotes: 1

axiac
axiac

Reputation: 72177

However, none of these things solve my problem. Furthermore, no matter what I do, nothing except the master branch shows up when I list my branches

From the output you posted, it seems the branch you are looking for is not present on the remote server.

However, if it is present on the remote server (let's say its name is my_desired_branch) you can make a local branch (having the same name) that points to the remote branch using the commands:

git branch my_desired_branch origin/my_desired_branch 
git checkout my_desired_branch

You can combine both of the commands above using git checkout -b:

git checkout -b my_desired_branch origin/my_desired_branch 

If the local branch my_desired_branch already exists but it is not connected to the remote branch you can reconnect them using:

git branch -u origin/my_desired_branch my_desired_branch

For more help about using git branch you can read the documentation or run git help branch on your command line.

Upvotes: 2

Related Questions