Skip to content
Latchkey

git fetch Command: Update Refs in CI

git fetch downloads new commits, branches, and tags from a remote without changing your working tree.

CI often clones shallow for speed, then fetches more history when a step needs it. fetch is also how runners refresh refs and tags before tagging or diffing.

Common flags

  • --depth N - limit (or set) the number of commits fetched
  • --unshallow - convert a shallow clone into a full clone with complete history
  • --tags - fetch all tags from the remote in addition to branches
  • --prune / -p - delete local remote-tracking refs that no longer exist on the remote
  • --no-tags - do not fetch any tags
  • --force / -f - overwrite local refs even on non-fast-forward updates

Example

shell
# Deepen a shallow CI clone so git describe and diffs work
git fetch --unshallow --tags origin
# Or fetch just a base branch for a diff
git fetch --depth=1 origin main

In CI

If a tool fails with "fatal: no merge base" or a broken git describe, the clone is shallow. Run git fetch --unshallow (or set fetch-depth: 0 in your checkout action) to restore history. Use --prune on long-lived runners so deleted remote branches do not linger.

Key takeaways

  • git fetch --unshallow restores full history when a shallow clone is too thin for a step.
  • fetch-depth: 0 in CI checkout actions maps to fetching all history.
  • Use --prune to keep remote-tracking refs accurate on reused runners.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →