# git fetch Command: Update Refs in CI

> git fetch downloads objects and refs from a remote without merging. Reference for --depth, --unshallow, --tags, and --prune as used to deepen shallow CI clones.

Source: https://latchkey.dev/learn/command-reference/git-fetch-command-reference  
Updated: 2026-06-26

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
```

> `git rev-parse --abbrev-ref HEAD` returns the literal string `HEAD` on a detached checkout rather than a branch name. On GitHub Actions read `github.ref_name` instead; the git command cannot know what it was checked out for.

## FAQ

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

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
