LinuxCommandLibrary

git-fetch

Download objects and refs from another repository

TLDR

Fetch the latest changes from the default remote upstream repository (if set)

$ git fetch
copy

Fetch new branches from a specific remote upstream repository
$ git fetch [remote_name]
copy

Fetch the latest changes from all remote upstream repositories
$ git fetch --all
copy

Also fetch tags from the remote upstream repository
$ git fetch [[-t|--tags]]
copy

Delete local references to remote branches that have been deleted upstream
$ git fetch [[-p|--prune]]
copy

Deepen current shallow branch by 2 commits
$ git fetch --deepen 2
copy

Update the main branch without switching to it (equivalent to git pull)
$ git fetch [origin] main:main
copy

SYNOPSIS

git fetch [options] [repository] [refspec…]

PARAMETERS

--all
    Fetch from all configured remotes

--multiple
    Allow multiple repository arguments

--dry-run
    Dry run, show what would be fetched without updating

-p, --prune
    Prune remote-tracking branches no longer on remote

-t, --tags
    Fetch all tags from remote (default: only those reachable from fetched heads)

--no-tags
    Disable tag fetching

-v, --verbose
    Be more verbose

-q, --quiet
    Be quiet

--depth=<n>
    Shallow fetch, limit history depth to n commits

-f, --force
    Force overwrite local refs

-k, --keep
    Keep downloaded pack

--update-head-ok
    Allow refs under HEAD to be updated

--no-auto-gc
    Disable automatic garbage collection

-u, --upload-pack=<exec>
    Specify path to upload-pack

DESCRIPTION

git fetch is a Git command that downloads commits, files, and references from a remote repository into your local repository, updating your remote-tracking branches (like origin/main). Unlike git pull, it does not merge changes into your current working branch, allowing you to review fetched updates first via git log or git diff before integrating them with git merge or git rebase.

This safe approach prevents accidental merges and supports workflows like reviewing pull requests. By default, it fetches from the origin remote using tracked branches. Specify a repository URL or remote name (e.g., git fetch upstream) to pull from others. Refspecs like main:local-main control mapping.

Key benefits include shallow fetches for large repos (--depth), pruning stale refs (--prune), and verbose output for debugging. It's essential for collaboration, ensuring your local repo mirrors remote state without altering your work. Common in CI/CD pipelines and before git pull. Use git fetch --all for multiple remotes.

CAVEATS

Does not merge changes; use git merge or git rebase afterward. Fails if local refs can't fast-forward without --force. Shallow clones limit history visibility.

COMMON USAGE

git fetch origin — Fetch from default remote.
git fetch --prune origin main — Fetch main and prune stale refs.

REFSPEC FORMAT

+src:dst maps src ref to dst; + forces update. Example: git fetch origin main:local-main.

HISTORY

Introduced in Git 0.99 (2005) by Linus Torvalds as core plumbing command. Evolved with remotes support in 1.0 (2006); prune and shallow options added later for efficiency.

SEE ALSO

git pull(1), git merge(1), git push(1), git remote(1), git ls-remote(1)

Copied to clipboard