BudiBadu Logo
Samplebadu

Git by Example: Fetch Updates

2.43.0

Learn how to safely download changes from a remote repository without merging them. This example explains the difference between git fetch and git pull.

Code

# 1. Fetch changes from the default remote (origin)
git fetch

# 2. Fetch from a specific remote
git fetch upstream

# 3. Check status after fetching
git status

# Output:
# On branch main
# Your branch is behind 'origin/main' by 5 commits, and can be fast-forwarded.
#   (use "git pull" to update your local branch)

# 4. Inspect the fetched changes (optional)
git log HEAD..origin/main --oneline

Explanation

The git fetch command is the safest way to download changes from a remote repository. Unlike git pull, which downloads and merges changes immediately, git fetch only downloads the new data (commits, files, refs) to your local repository. It updates your "remote-tracking branches" (like origin/main) but leaves your actual working files and local branches untouched.

This separation is powerful because it allows you to see what others have been working on without risking a sudden merge conflict or disrupting your current workflow. You can fetch the updates, inspect them using git log or git diff, and then decide when and how to integrate them (e.g., via merge or rebase).

Think of git fetch as "checking for updates" and downloading them to a waiting area. It gives you the full picture of the project's progress while keeping your own work isolated until you are ready to sync up.

  • Downloads remote changes without merging
  • Updates remote-tracking branches
  • Safe and non-destructive
  • Allows inspection before integration

Code Breakdown

2
Downloads the latest commits and branches from 'origin'. No files in your working directory are changed.
8
After fetching, git status is smart enough to tell you that your local branch is "behind" the remote branch, letting you know there are updates waiting.
14
This command shows the commits that exist on the remote branch ('origin/main') but not yet on your local branch ('HEAD').