git fetch
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>]Options
Choosing the Right Option
Fetch vs Pull
Understanding the key difference between fetch and pull
| Command | Downloads Commits | Updates Local Branch | Safe to Run Anytime |
|---|---|---|---|
git fetch | Yes | No - only remote-tracking | Yes - never changes working dir |
git pull | Yes | Yes - merges into current | Caution - may cause conflicts |
git pull --rebase | Yes | Yes - rebases current | Caution - rewrites local commits |
Fetch Options for Cleanup
Options for keeping your repository tidy
| Command | Removes Stale Branches | Fetches Tags | Use Case |
|---|---|---|---|
git fetch | No | Some (reachable) | Standard update |
git fetch --prune | Yes | Some (reachable) | Clean up deleted branches |
git fetch --tags | No | All tags | Get release tags |
git fetch --prune --tags | Yes | All tags | Clean branches + get all tags (local-only tags kept) |
Fetch Scope
Control what gets fetched
| Command | Fetches From | Best For |
|---|---|---|
git fetch | Default remote (origin) | Most common use |
git fetch origin | Specific remote | When you have multiple remotes |
git fetch --all | All remotes | Syncing fork with upstream |
git fetch origin main | Single branch only | Minimal download |
Examples
Fetch from default remote
git fetchDownloads new commits from the default remote (usually 'origin') without merging.
Fetch from specific remote
git fetch originExplicitly fetch from the 'origin' remote.
Fetch all remotes
git fetch --allFetch from all configured remotes at once.
Fetch and prune stale branches
git fetch --pruneFetch and remove remote-tracking branches that no longer exist on the remote.
Workflow Examples
git fetch originDownload remote changes without merging
git log main..origin/mainSee commits on remote not in your local
git diff main...origin/mainReview the code changes made on the remote
git merge origin/mainMerge when you're ready
git remote add upstream <original-repo-url>Add original repo as upstream (once)
git fetch upstreamGet latest from original repo
git checkout mainSwitch to your main branch
git merge upstream/mainMerge upstream changes into your fork
git push origin mainPush updated fork to your GitHub
git fetch --pruneFetch and remove stale remote-tracking branches
git branch -vvSee local branches and their tracking status
git branch -d old-featureDelete 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
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.
Fetch only updates remote-tracking branches (like origin/main), not your local branches. You need to merge or rebase to update your local branch.
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.
Fetch when you want to review changes first or when working on important branches. Pull for quick updates when you trust the incoming changes.
Use 'git fetch origin branch-name' to fetch only that branch. Useful for large repos where you don't need everything.
It removes remote-tracking branches that no longer exist on the remote. Keeps your repo clean when teammates delete merged branches.