Skip to content

fetch Git Command Guide

The git fetch command is used to download objects and refs from one or more remote repositories, along with the objects necessary to complete their histories. It updates remote-tracking branches and stores fetched information in .git/FETCH_HEAD.

Terminal window
git fetch [<options>] [<repository> [<refspec>...]]
OptionDescription
—allFetch from all remotes
—appendAppend to FETCH_HEAD instead of overwriting
—atomicUse atomic transaction for ref updates
—depth=Limit fetch depth
—deepen=Deepen shallow repository
—shallow-since=Include commits after date in shallow repo
—shallow-exclude=Exclude commits from ref in shallow repo
—unshallowConvert shallow to full repository
—dry-runShow what would be done
—porcelainMachine-readable output
-f, —forceOverride safety checks
-k, —keepKeep downloaded pack
—multipleFetch from multiple repositories
-p, —pruneRemove obsolete remote-tracking refs
-P, —prune-tagsRemove obsolete local tags
-t, —tagsFetch all tags
-n, —no-tagsDon’t fetch tags automatically
—prefetchStore refs in refs/prefetch namespace
—write-commit-graphWrite commit-graph after fetching
—refetchRe-fetch everything for partial clones
ParameterDescription
[]Remote repository to fetch from (default: origin)
[…]Refs to fetch (default: configured fetch refspec)
Terminal window
git fetch

Fetch changes from the default remote (usually origin).

Terminal window
git fetch origin

Fetch from the origin remote.

Terminal window
git fetch origin main

Fetch only the main branch from origin.

Terminal window
git fetch --all

Fetch from all configured remotes.

Terminal window
git fetch --prune

Remove local remote-tracking refs that no longer exist remotely.

Terminal window
git fetch --depth=1 origin

Fetch with shallow history of depth 1.

Terminal window
git fetch --dry-run origin

Show what would be fetched without making changes.

Terminal window
git fetch --tags origin

Fetch all tags from origin in addition to normal refs.

Terminal window
git fetch --unshallow origin

Convert a shallow clone to a full repository.

Terminal window
git fetch --atomic --write-commit-graph origin

Use atomic transaction and write commit-graph for improved performance.

How do I update my local branches with remote changes?

Section titled “How do I update my local branches with remote changes?”

To update your local branches with remote changes, run:

Terminal window
git fetch origin

To fetch from all configured remotes, use:

Terminal window
git fetch --all

How do I clean up old remote-tracking branches?

Section titled “How do I clean up old remote-tracking branches?”

To clean up old remote-tracking branches, use —prune:

Terminal window
git fetch --prune origin

To preview what git fetch will do, use —dry-run:

Terminal window
git fetch --dry-run origin

To fetch a shallow copy with limited history, use —depth:

Terminal window
git fetch --depth=<n> origin

To make a shallow repository full, use —unshallow:

Terminal window
git fetch --unshallow origin

How do I force a fetch that would otherwise be rejected?

Section titled “How do I force a fetch that would otherwise be rejected?”

To force a fetch that would be rejected due to safety checks, use —force:

Terminal window
git fetch --force origin

To fetch all tags from the remote, use —tags:

Terminal window
git fetch --tags origin

To prevent automatic tag fetching, use —no-tags:

Terminal window
git fetch --no-tags origin
  1. Updating repositories with remote changes before merging
  2. Setting up tracking branches for collaboration
  3. Managing multiple remote repositories in a project
  4. Implementing continuous integration fetch operations
  5. Creating backup and mirror repositories
  6. Supporting distributed development workflows