Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Remotes and Collaboration

git fetch command

Reference for git fetch: download new commits and update remote-tracking branches without touching your work. Options --all, --prune, --tags, --depth and refspecs.

git fetch contacts another repository, downloads the objects you do not have yet, and updates your remote-tracking branches (origin/main and friends) to match. It never changes your local branches, your index or your working tree, so it is always safe to run.

Fetching is the first half of git pull; the second half is a merge or rebase. Many people fetch first, look at what arrived (git log main..origin/main), and then integrate on purpose.

Terminal
git fetch [<options>] [<remote> [<refspec>...]]
git fetch --all
git fetch <group>

Try it

Bash Fetch moves origin/main, not main

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Prune, tags and FETCH_HEAD

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

<remote> is a remote name or a URL; without it Git uses the upstream of the current branch, or origin. Each <refspec> has the form [+]<src>:<dst>; see refspecs. With no refspec Git uses remote.<name>.fetch, normally +refs/heads/*:refs/remotes/origin/*.

Option Description
--all Fetch from every configured remote.
-p, --prune Delete remote-tracking branches that no longer exist on the remote. -P/--prune-tags also prunes tags.
-t, --tags Fetch all tags in addition to the usual ones. -n/--no-tags fetches none. By default tags that point into fetched history come along.
--depth=<n>, --deepen=<n>, --unshallow Create, extend or remove a shallow history. --shallow-since=<date> and --shallow-exclude=<ref> also exist.
--filter=<spec> Partial clone filter, for example blob:none.
-f, --force Allow non-fast-forward updates of the destination refs (what a leading + in a refspec does).
--dry-run Show what would be fetched and change nothing.
-q, -v Less or more output.
--set-upstream Set the upstream configuration for the current branch after fetching.
--recurse-submodules[=yes|on-demand|no] Also fetch changed submodules.
-j <n>, --jobs=<n> Fetch from several remotes and submodules in parallel.
--negotiation-tip=<commit> Report only these commits as local tips when negotiating, which helps in huge repositories.
--auto-maintenance, --write-commit-graph Control the housekeeping that may run afterward.

Usage notes

  • FETCH_HEAD (the file .git/FETCH_HEAD) records the tips fetched by the last command, which is how git pull knows what to merge. git log FETCH_HEAD is handy after fetching a URL that has no remote name.
  • Output lines mark each ref update: * new, a range a..b for fast-forward, + or ... for a forced update, - pruned, = up to date.
  • fetch.prune=true and remote.<name>.prune=true make pruning automatic; fetch.pruneTags does the same for tags.
  • Fetching a branch without a destination (git fetch origin topic) always writes FETCH_HEAD; Git also opportunistically updates origin/topic when the remote's fetch refspec covers it.
  • You cannot fetch into the branch that is checked out except with --update-head-ok; that is why destinations are normally remote-tracking refs.
  • Fetch can be run from cron or an editor in the background; it never causes conflicts.