Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git shallow clone (clone --depth) misses remote branches

People also ask

Does git clone get all remote branches?

git clone downloads all remote branches but still considers them "remote", even though the files are located in your new repository. There's one exception to this, which is that the cloning process creates a local branch called "master" from the remote branch called "master".

Does cloning a repo get all branches?

The idea is to use the git-clone to clone the repository. This will automatically fetch all the branches and tags in the cloned repository. To check out the specific branch, you can use the git-checkout command to create a local tracking branch.

How do I clone a specific remote branch?

In order to clone a specific branch, you have to execute “git branch” with the “-b” and specify the branch you want to clone. $ git clone -b dev https://github.com/username/project.git Cloning into 'project'... remote: Enumerating objects: 813, done.


After doing a shallow clone, to be able to checkout other branches from remote,

  1. Run (thanks @jthill):

    git remote set-branches origin '*'
    
  2. After that, do a git fetch -v

  3. Finally git checkout the-branch-i-ve-been-looking-for


Step 1 can also be done manually by editing .git/config.

For instance, change the folloing line from:

fetch = +refs/heads/master:refs/remotes/origin/master

to (replace master with *):

fetch = +refs/heads/*:refs/remotes/origin/*

From reading the responses and the comment from @jthill, the thing that worked best for me was to use the set-branches option on the git remote command:

$ git clone --depth 1 https://github.com/dogescript/dogescript.git
$ git remote set-branches origin 'remote_branch_name'
$ git fetch --depth 1 origin remote_branch_name
$ git checkout remote_branch_name

This changes the list of branches tracked by the named remote so that we can fetch and checkout just the required branch.


The behavior is correct, after the last revision the master-branch is (since this is the primary remote's HEAD) the only remote-branch in the repository:

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

The full clone offers new (all) branches:

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

Shallow clones

Due to the shallow-description in the technical documentation, a "git-clone --depth 20 repo [...] result[s in] commit chains with a length of at most 20." A shallow clone therefore should contain the requested depth of commits, from the tip of a branch.

As - in addition - the documentation of git clone for the --single-branch-option describes:

"Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote's HEAD points at. When creating a shallow clone with the --depth option, this is the default, unless --no-single-branch is given to fetch the histories near the tips of all branches."

Therefore a shallow clone (with the depth-option) only fetches only one single branch (at your requested depth).


Unfortunately both options (--depth and --single-branch) have been faulty in the past and the use of shallow clones implicits unresolved problems (as you can read in the link I posted above), which is caused by the given history-rewrite. This leads in overall to somewhat complicated behavior in special cases.