5

After I did a clone of a repo, a new branch test has been added to the origin remote. But I still see:

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

What git command/s should I use to get the following output:

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

The git checkout remote branch question is not my question because that is happening after I see all the remote branch references.

I cannot clone the repo again because I have my local changes/commits in it.

So, how to bring the new remote branch references to my repo using git commands?

I am using a BitBucket repo that has the following branches: branches


Trials

git fetch does not work:

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git fetch
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git fetch origin 
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
Community
  • 1
  • 1
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168

2 Answers2

4

A simple git fetch should be enough.

That will bring origin/test to your repo.

Then a git checkout -b test origin/test will declare that branch locally.


The OP Gabriel Petrovay confirms (in the comments) the source of the problem:

There is a slight change in the git config file.

  • In the newly cloned repo [remote "origin"] has fetch = +refs/heads/*:refs/remotes/origin/*
  • but the old repo has fetch = +refs/heads/master:refs/remotes/origin/master.

I kind of feel this is the issue.

It is indeed.

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"

That should solve it.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • @GabrielPetrovay if you clone again that repo elsewhere (just for test), do you see `origin/test` then? (`git branch -av` in that new clone). – VonC Apr 30 '14 at 11:45
  • @GabrielPetrovay does `git remote -v` in your current repo shows the right url to the right remote repo with the test branch? – VonC Apr 30 '14 at 11:46
  • Yes I just cloned to a new location and I see them: * master remotes/origin/HEAD -> origin/master remotes/origin/crud_links_ui remotes/origin/form_update remotes/origin/master See the image in the post. (`test` branch was just to simplify the question) – Gabriel Petrovay Apr 30 '14 at 11:47
  • @GabrielPetrovay does `git remote -v` returns the same in the `newly cloned repo`, and in your `original repo`? – VonC Apr 30 '14 at 11:49
  • Yes they are the same: `$ git remote -v`: `origin git@bitbucket.org:user/repo.git (fetch)` and `origin git@bitbucket.org:user/repo.git (push)` – Gabriel Petrovay Apr 30 '14 at 11:51
  • @GabrielPetrovay in both local repos, what does a `git ls-remote` return? – VonC Apr 30 '14 at 11:52
  • In both I get all the branches listed with their top commit: `HEAD`, `refs/heads/crud_links_ui`, `refs/heads/form_update`, `refs/heads/master` – Gabriel Petrovay Apr 30 '14 at 11:55
  • @GabrielPetrovay then I don't see any reason why a git fetch wouldn't... fetch. – VonC Apr 30 '14 at 11:56
  • There is a slight change in the git config file. In the newly cloned repo `[remote "origin"]` has `fetch = +refs/heads/*:refs/remotes/origin/*` but the old repo has `fetch = +refs/heads/master:refs/remotes/origin/master`. I kind of feel this is the issue. – Gabriel Petrovay Apr 30 '14 at 11:58
  • @GabrielPetrovay yes, I have edited the answer with the right command. – VonC Apr 30 '14 at 12:06
  • Thanks @VonC for helping me debug this! – Gabriel Petrovay Apr 30 '14 at 12:07
  • @GabrielPetrovay no problem, you are welcome. I prefer using `git config` command to change the config, rather than editing directly the `.gitconfig` file. Hence the edit in my answer. – VonC Apr 30 '14 at 12:08
2

Found the problem!!!

A git clone with a depth 1:

git clone --depth 1 <repo>

will only need the master branch reference and the git repote config generated for this repo will be:

[remote "origin"]                                                                                                
   url = git@bitbucket.org:jillix/cctool.git
   fetch = +refs/heads/master:refs/remotes/origin/master

instead of:

[remote "origin"]                                                                                                
   url = git@bitbucket.org:jillix/cctool.git
   fetch = +refs/heads/*:refs/remotes/origin/*

This will make the fetch fail in bringing all the remote references.

Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168