Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout new remote branch when cloning with depth 1 option

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
like image 427
4 revs, 2 users 76% Avatar asked Sep 30 '22 22:09

4 revs, 2 users 76%


2 Answers

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.

like image 116
VonC Avatar answered Oct 10 '22 20:10

VonC


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 = [email protected]:jillix/cctool.git
   fetch = +refs/heads/master:refs/remotes/origin/master

instead of:

[remote "origin"]                                                                                                
   url = [email protected]:jillix/cctool.git
   fetch = +refs/heads/*:refs/remotes/origin/*

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

like image 41
4 revs, 2 users 76% Avatar answered Oct 10 '22 20:10

4 revs, 2 users 76%