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 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.
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
git clone Command: Clone Repos in CIgit clone copies a remote repository into a new directory. Reference for --depth, --branch, --filter, and --r…
git describe Command: Version Strings in CIgit describe builds a human-readable version from tags. Reference for --tags and --always to derive build ver…
git rev-list Command: Count Commits in CIgit rev-list lists commit objects in reverse chronological order. Reference for --count to derive monotonic b…
git diff Command: Detect Changes in CIgit diff shows changes between commits or the working tree. Reference for --name-only, --stat, and --exit-cod…