Git Notes
The git fetch command downloads new data from a remote repository without modifying your working directory or current branch. Think of it as checking your...
The git fetch command downloads new data from a remote repository without modifying your working directory or current branch. Think of it as checking your mailbox without opening any letters — you know what has arrived, but nothing changes until you decide to act on it. This makes fetch the safest way to stay informed about what is happening on the remote repository.
Understanding Fetch vs Pull
The distinction between git fetch and git pull confuses many beginners, but it is straightforward once you understand what each does:
- git fetch — Downloads new commits, branches, and tags from the remote. Updates your remote-tracking branches (like
origin/main). Does NOT change your local branches or working directory. - git pull — Runs
git fetchfollowed bygit merge(orgit rebase). Downloads AND integrates remote changes into your current branch.
Fetch gives you control. You can see what changed remotely, decide whether you want those changes, and merge them on your own terms.
Basic Fetch Usage
Fetch all updates from the default remote (origin):
git fetchFetch from a specific remote:
git fetch originFetch a specific branch:
git fetch origin developFetch all remotes at once:
git fetch --allWhat Happens After Fetching
After running git fetch, your remote-tracking branches are updated. You can inspect what changed:
# See what branches exist on the remote
git branch -r
# Compare your local main with the fetched remote main
git log main..origin/main --oneline
# See a diff of what changed
git diff main origin/main
# View detailed commit information
git log origin/main --oneline -10The key insight is that origin/main on your machine is a snapshot of where main is on the remote. After fetch, this snapshot is current, but your local main has not moved.
Practical Workflow with Fetch
Here is a professional workflow using fetch:
This approach lets you review remote changes before integrating them, reducing the chance of surprises.
Fetching and Remote-Tracking Branches
When you fetch, Git updates references in the refs/remotes/ namespace:
git fetch origin
# These are now updated:
# refs/remotes/origin/main
# refs/remotes/origin/develop
# refs/remotes/origin/feature/new-apiYou can check out a remote-tracking branch to inspect it:
git checkout origin/feature/new-apiThis puts you in detached HEAD state. To work on it locally:
git checkout -b feature/new-api origin/feature/new-apiFetch Options and Flags
Prune deleted remote branches:
git fetch --pruneThis removes remote-tracking branches that no longer exist on the remote. Without pruning, your local list of remote branches can become stale.
Fetch tags:
git fetch --tagsDownloads all tags from the remote. By default, fetch only downloads tags that point to fetched commits.
Dry run — see what would be fetched:
git fetch --dry-runShows what would be downloaded without actually downloading anything.
Fetch in Team Environments
In a team setting, fetch is essential for awareness:
# See all branches your teammates are working on
git fetch --all
git branch -r
# Check if someone updated the branch you are about to merge
git fetch origin
git log origin/feature/auth --oneline -5
# Before starting a code review, get the latest version
git fetch origin
git checkout origin/feature/review-thisAutomating Fetch
Many developers configure their IDE or terminal to fetch periodically:
# Git configuration for auto-fetch
git config --global fetch.prune true # Always prune on fetch
git config --global fetch.pruneTags true # Prune tags tooSome Git GUIs (like VS Code's Source Control) fetch automatically in the background, keeping your remote-tracking branches always current.
Common Scenarios
Scenario: Check if your branch is behind
git fetch origin
git status
# Your branch is behind 'origin/main' by 3 commits, and can be fast-forwarded.Scenario: A colleague says they pushed a new branch
git fetch origin
git branch -r | grep colleague-feature
# origin/colleague-feature
git checkout -b colleague-feature origin/colleague-featureScenario: See all new activity across all branches
git fetch --all
git log --all --oneline --graph --since="yesterday"Fetch vs Pull: When to Use Which
Use fetch when:
- You want to inspect changes before integrating them
- You are checking if a remote branch exists
- You want to compare local and remote state
- You are in the middle of work and do not want to trigger a merge
Use pull when:
- You are about to start work and want the latest code immediately
- You trust the incoming changes (like pulling main first thing in the morning)
- You want the convenience of fetch + merge in one command
Key Takeaways
Git fetch is the safe, non-destructive way to download remote updates. It keeps your remote-tracking branches current without touching your local work. Use it regularly to stay informed about team activity, and combine it with manual merging for complete control over how and when remote changes enter your local branches.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Fetch.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, collaboration, fetch, git fetch
Related Git & GitHub Topics