Git & GitHub ยท Chapter 28 of 42

git fetch

`git fetch` downloads new commits, branches, and tags from a remote repository, but does not merge them into your local branches. It lets you see what's changed before deciding to integrate it.

This makes fetch a safer alternative to pull when you want to review incoming changes first, using commands like `git log origin/main` or `git diff main origin/main`.

Syntax
git fetch origin
git diff main origin/main

Fetching updates

Run `git fetch origin` to download the latest commits and branches from the origin remote without changing your working files.

Reviewing fetched changes

After fetching, compare your branch to the remote with `git diff main origin/main` or `git log main..origin/main` to see new commits before merging.

Example 1 (bash)
git fetch origin
Output
remote: Enumerating objects: 8, done.
   a3f5c9e..9c2e1aa  main -> origin/main

Downloads new commits from origin into the local origin/main tracking branch, without merging.

Example 2 (bash)
git log main..origin/main --oneline
Output
9c2e1aa Fix crash when cart is empty

Shows commits that exist on the remote main but not yet on your local main branch.

Key points

  • git fetch downloads changes without merging them.
  • It updates remote-tracking branches like origin/main.
  • You can review fetched changes before merging manually.
  • git pull = git fetch + git merge combined.
๐Ÿ’ก Note: Use git fetch when you want full control over when remote changes get merged into your work.

๐Ÿ“ Quick Quiz

1. What does git fetch do differently from git pull?

2. After fetching, where do new remote commits appear?

3. Why might you prefer fetch over pull?