Why does my PR build fetch all remote branches and tags, ignoring CM_CLONE_DEPTH?
Last updated: October 15, 2026
Context
When using CM_CLONE_DEPTH: "1" in your codemagic.yaml, push-triggered builds clone only a shallow copy of the repository, completing quickly. However, pull request (PR) builds may still take significantly longer during the "Fetching app sources" step. This happens because Codemagic performs an additional, unbounded git fetch source to retrieve the PR's source branch for merging — downloading all remote branches and tags regardless of the configured clone depth.
Answer
This behavior is intentional. For PR builds, Codemagic clones the destination branch and then merges the source branch into it. Applying --depth 1 to the second fetch is not safe, because if both sides contain only their tip commit, Git may be unable to find their common ancestor, causing the merge to fail. The source fetch therefore needs enough history to reach the merge base.
However, the fetch does not need to include history from every unrelated release or feature branch. You can exclude those branch namespaces before Codemagic runs its internal fetch by using a pre_clone_scripts block in your codemagic.yaml:
pre_clone_scripts:
- name: Limit PR source fetch
script: |
git config --global remote.source.tagOpt --no-tags
git config --global --add remote.source.fetch '^refs/heads/release/*'
git config --global --add remote.source.fetch '^refs/heads/feature/*'This configuration tells Git to skip tags and exclude the specified branch namespaces when Codemagic performs its internal git fetch source. A few things to keep in mind:
Start with the branch prefixes that account for the most unnecessary refs (e.g.,
release/*), then add other stale branch prefixes as needed.Do not exclude any prefix that could contain the PR source branch, as this would prevent the merge from completing successfully.
This will not make the selected source branch shallow, but it will avoid fetching large amounts of unrelated history that would otherwise add significant time to the build.