2

Suppose I have a remote git repo which is extremely big (e.g. > 10 GiB) and the network connection is unstable which means any attempt of fetching the whole repo is deemed to fail (i.e., not possible to execute anything like git fetch --all or git fetch --unshallow).

I however managed to get a shallow clone by issuing git clone --depth 1. However, this shallow clone does not have any branches other than master. Neither git checkout <branch> nor git checkout -b <branch> --track <origin/branch> works. No branch is showing for git branch -r either.

It is possible to run git fetch --depth 1 origin <branch>, but after that the desired branch does not show in git branch or git branch -r either, and any attempt to checkout that branch still fails.

How can I checkout a remote branch in this situation?

Remarks,

git clone -b <branch> --depth 1 does work, but even a shallow clone takes hours to complete (if it completes at all), so I would like to reuse as many local objects as possible.

Tippisum
  • 161
  • 9
  • Sound like your repo has many binary objects. Git can not do much with binary objects, so you have to bear the data transfer overheads. – oakad Aug 26 '21 at 06:38
  • 2
    Note that Git doesn't have "shallow checkout" (it does have "sparse checkout" but that's something else entirely). Your issue here is due to the fact that a shallow clone is, by default, also a *single-branch* clone. See the duplicates I've linked. However, you may just want to add `--no-single-branch` to your initial clone call. – torek Aug 26 '21 at 07:27
  • You might also consider making one full clone once, and then using `--reference` (with or without `--dissociate`). These get a little more complicated though. If you do make a reference clone to use, it's a good idea to update it periodically, so that the reference does not get too far behind the original. – torek Aug 26 '21 at 07:30
  • 1
    It might also be worth trying out [partial cloning](https://git-scm.com/docs/partial-clone). – Enrico Campidoglio Aug 26 '21 at 07:33
  • 3
    You can use `git fetch --depth 1 origin :refs/heads/` or `git fetch --depth 1 origin :refs/remotes/origin/` to create the branch or remote branch. If you just want to checkout the fetched commit, you could also use `git fetch --depth 1 origin && git checkout FETCH_HEAD`. – ElpieKay Aug 26 '21 at 08:16

0 Answers0