git fetch

Sharing and Updating

Download objects and refs from another repository


The 'git fetch' command downloads commits, files, and refs from a remote repository into your local repo. It lets you see what others have been working on without merging those changes into your local branches. Unlike 'git pull', fetch is safe - it never changes your working directory or current branch. After fetching, you can review the changes and decide whether to merge them.

Synopsis

git fetch [<options>] [<repository> [<refspec>...]]
git fetch [<options>] <group>
git fetch --multiple [<options>] [(<repository> | <group>)...]
git fetch --all [<options>]
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (4)

Fetch from all configured remotes at once. Useful when you have multiple remotes like origin and upstream.

When neededHandy with several remotes (fork + upstream); plain fetch covers one

Example

git fetch --all

Before fetching, remove any remote-tracking branches that no longer exist on the remote. Keeps your repository clean.

RecommendedRemoves stale origin/* refs for branches deleted on the remote

Example

git fetch --prune

Fetch all tags from the remote, in addition to whatever else would be fetched. Useful for getting release tags.

When neededTags on fetched commits already arrive; use it for tags on other history

Example

git fetch --tags

Show what would be done, without making any changes. Useful to preview what fetch will download.

When neededPreviews which refs would update without changing anything

Example

git fetch --dry-run

Intermediate (1)

Limit fetching to the specified number of commits from the tip of each remote branch. In a shallow clone, this deepens or shortens the history.

When neededSaves time in CI or huge repos, but shallow history limits log and merge-base

Example

git fetch --depth=1

Choosing the Right Option

Fetch vs Pull

Understanding the key difference between fetch and pull

CommandDownloads CommitsUpdates Local BranchSafe to Run Anytime
git fetchYesNo - only remote-trackingYes - never changes working dir
git pullYesYes - merges into currentCaution - may cause conflicts
git pull --rebaseYesYes - rebases currentCaution - rewrites local commits

Fetch Options for Cleanup

Options for keeping your repository tidy

CommandRemoves Stale BranchesFetches TagsUse Case
git fetchNoSome (reachable)Standard update
git fetch --pruneYesSome (reachable)Clean up deleted branches
git fetch --tagsNoAll tagsGet release tags
git fetch --prune --tagsYesAll tagsClean branches + get all tags (local-only tags kept)

Fetch Scope

Control what gets fetched

CommandFetches FromBest For
git fetchDefault remote (origin)Most common use
git fetch originSpecific remoteWhen you have multiple remotes
git fetch --allAll remotesSyncing fork with upstream
git fetch origin mainSingle branch onlyMinimal download

Examples

Fetch from default remote

git fetch

Downloads new commits from the default remote (usually 'origin') without merging.


Fetch from specific remote

git fetch origin

Explicitly fetch from the 'origin' remote.


Fetch all remotes

git fetch --all

Fetch from all configured remotes at once.


Fetch and prune stale branches

git fetch --prune

Fetch and remove remote-tracking branches that no longer exist on the remote.

Workflow Examples

1
git fetch origin

Download remote changes without merging

2
git log main..origin/main

See commits on remote not in your local

3
git diff main...origin/main

Review the code changes made on the remote

4
git merge origin/main

Merge when you're ready

1
git remote add upstream <original-repo-url>

Add original repo as upstream (once)

2
git fetch upstream

Get latest from original repo

3
git checkout main

Switch to your main branch

4
git merge upstream/main

Merge upstream changes into your fork

5
git push origin main

Push updated fork to your GitHub

1
git fetch --prune

Fetch and remove stale remote-tracking branches

2
git branch -vv

See local branches and their tracking status

3
git branch -d old-feature

Delete local branches that tracked deleted remotes

Troubleshooting

fatal: 'origin' does not appear to be a git repository

Cause: No remote named 'origin' is configured

Check remotes with 'git remote -v'. Add one with 'git remote add origin <url>'

Fetched but my local branch didn't update

Cause: Fetch only updates remote-tracking branches, not local branches

This is expected! Run 'git merge origin/main' or 'git pull' to update your local branch

error: cannot lock ref - unable to update local ref

Cause: A stale remote-tracking ref clashes with a new branch name (e.g. 'feature' vs 'feature/login')

Run 'git remote prune origin' or 'git fetch --prune' to remove the stale refs, then fetch again

Still seeing deleted remote branches

Cause: Need to prune stale remote-tracking branches

Use 'git fetch --prune' or 'git remote prune origin'

Fetch is very slow or downloads too much

Cause: Fetching full history when you only need recent commits

Use 'git fetch --depth=1' for shallow fetch, or fetch specific branch only

Tips & Pitfalls

Best Practices

  • ✓Fetch before starting work to see if there are remote changes you should know about
  • ✓Use --prune regularly to keep your repository clean of stale branches
  • ✓Prefer fetch + merge over pull when you want to review changes before integrating
  • ✓Set up 'git config fetch.prune true' to always prune on fetch
  • ✓Use 'git fetch --all' when working with forks to sync both origin and upstream

Common Mistakes

  • ✗Expecting fetch to update your working directory - it only updates remote-tracking branches, not your local branches.
  • ✗Forgetting to merge or rebase after fetching - fetch downloads but doesn't integrate changes.
  • ✗Not using --prune regularly, leading to stale remote-tracking branches cluttering your repo.
  • ✗Confusing fetch with pull - pull = fetch + merge, while fetch is just the download step.

FAQ

A:

Fetch downloads commits but doesn't change your local branches. Pull = fetch + merge (or rebase). Fetch is safer because it lets you review changes before integrating.

A:

Fetch only updates remote-tracking branches (like origin/main), not your local branches. You need to merge or rebase to update your local branch.

A:

Use 'git log main..origin/main' to see commits on remote that aren't in your local branch, or 'git diff main...origin/main' to see the code changes made on the remote.

A:

Fetch when you want to review changes first or when working on important branches. Pull for quick updates when you trust the incoming changes.

A:

Use 'git fetch origin branch-name' to fetch only that branch. Useful for large repos where you don't need everything.

A:

It removes remote-tracking branches that no longer exist on the remote. Keeps your repo clean when teammates delete merged branches.