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
# 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 mainIn 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.
- 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 detachedKey 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.