This is not really a Git issue, but rather a GitLab-CI issue. However, these particular issues crop up in multiple CI/CD systems, always with the same general reason:
- Making a full clone of a large repository is slow.
- CI systems often start by cloning a repository.
- Hence, CI systems often start by making a less-than-full clone of the repository.
- Git offered just two ways to do this in the past:1 shallow clones and single-branch clones. In fact, they tend to go together: making a shallow clone, with
git clone --depth number
, also makes a single-branch clone unless you forcibly override Git's default by adding --no-single-branch
as well.
Now, the clone command in any given CI/CD system may be controllable, or may be done by the CI/CD system before any software knobs that are available to you-as-a-developer are provided. If you can affect the command, that's usually the way to go, but if not, you may still be able to correct the problem by running additional Git commands (this again depends on the CI/CD system):
git remote set-branches --add
allows you to add various branches to an otherwise single-branch clone;
git fetch --depth number
, git fetch --deepen number
, and git fetch --unshallow
are three ways to alter the depth of an existing shallow clone, or remove the depth restrictions entirely.
If your CI/CD system makes a shallow, single-branch clone, and you need a deeper clone with more remote-tracking names, you can get that using git remote
and/or git fetch
.
As I haven't used GitLab's CI system, I cannot provide a recipe here, but based on comments, it sounds like they use --depth 50
by default. If you have a depth knob, setting it to zero (0
) is the usual way to disable the --depth
argument, which also disables the single-branch-ness.
Do note that regardless of how the system clones your repository, you will generally have only one branch name, or perhaps even none (detached HEAD) when cloning via tag. A full clone will have a full set of remote-tracking names, while a single-branch clone will have only one remote-tracking name until you use git remote
to adjust this and run a subsequent git fetch
to add more remote-tracking names.
With GitHub Actions, the v2
checkout makes a shallow single-branch clone by default, while the v1
checkout makes a full clone.
1There is now a third method, the so-called partial clone with promisor remotes. This is arguably the right way for a CI system to do the trick, but partial clones are not very good yet, for a lot of reasons, so this probably has to wait for improvements to the implementation.
git fetch
first? – Nesselrodegit fetch
what ? By default gitlab first init the repo and does the git add remote and checkout the branch (that is merge request branch) . Do you mean I shouldgit fetch origin master
first before diff ? – Degenerationget fetch
by itself, without arguments, it will fetch all branches.By default gitlab first init the repo and does the git add remote and checkout the branch (that is merge request branch)
No, kind of, gitlab-ci ends up on detached head without branch checkout, but it depends on configuration. There's even a default number of commits checked in project configuration, I think default is 50. – Nesselrodegit checkout master
andgit pull
also. – Nesselrodegit fetch && git checkout ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} && git pull
, this worked. – Degeneration