Skip to content
LatchkeyLatchkey home

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.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

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.

Frequently asked questions

git fetch Command: Update Refs in CI?
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.
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.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card