Skip to content

Understanding Git Fetch# Understanding Git Fetch

You're working on your feature. You want to know what changed on remote. But you don't want to mess with your local files. You don't want to merge anything yet. Just check what's new.You're working on your feature. You want to know what changed on remote. But you don't want to mess with your local files. You don't want to merge anything yet. Just check what's new.

That's git fetch.

What Git Fetch Does

It downloads information from remote. New commits. New branches. New tags. Everything.- Retrieves the latest changes (commits, branches, tags) from a remote repository.

  • Updates your local metadata (remote tracking branches), but does not merge or apply these changes to your working branches.

But it doesn't change your files. Doesn't touch your working directory. Doesn't merge anything.- It’s safe: you can fetch as often as you like without worrying about overwriting local changes.

Think of it as reading the news. You're gathering information. Not taking action yet.> Analogy: If git pull is like receiving new mail and reading it right away, git fetch is like receiving new mail and putting it aside to read later.

flowchart LR

    A[Remote Repository] -->|git fetch| B[Local .git]## **2. Common Use Cases**

    B -.->|No changes| C[Working Directory]

    1. **Check for new commits** on the remote before merging changes locally.

    style A fill:#e1f5ff2. **Review differences** between your local branch and the remote branch.

    style B fill:#e8f5e93. **Fetch updates from multiple remotes** in complex setups (e.g., `origin` + `upstream`).

    style C fill:#f0f0f0

```---



Your working directory stays clean. You can review changes. Decide what to do next.## **3. Basic Usage**



## The Daily Command### **3.1 Fetch from the Default Remote**

```bash

This is what you'll use most of the time:git fetch

``bash- Fetches updates from **all configured remotes** (commonlyorigin`).

git fetch origin- If you have only one remote, it’s effectively the same as git fetch origin.

### **3.2 Fetch from a Specific Remote**

That's it. This fetches all branches from `origin`. Updates your local knowledge of remote branches.```bash

git fetch origin

After fetching, check what's new:```

- Fetches the latest updates **only** from the remote named `origin`.

```bash- Updates **all branches** in `origin` (e.g., `origin/main`, `origin/feature-xyz`).

git status- Does **not** update local branches automatically.

3.3 Fetch Only a Specific Branch

You'll see something like:```bash

git fetch origin main

On branch main- Fetches **only** the `main` branch from the remote named `origin`.

Your branch is behind 'origin/main' by 3 commits.- Updates **only** `origin/main`, without touching other branches.

  (use "git pull" to update your local branch)

```---



Or check specific differences:## **4. Differences at a Glance**



```bash| Command                 | Fetches Updates From | Updates Which Branches?               | Use Case                                              |

git log main..origin/main --oneline|-------------------------|----------------------|---------------------------------------|-------------------------------------------------------|

```| `git fetch`            | All remotes         | All remote tracking branches          | Check updates from all remotes                        |

| `git fetch origin`     | `origin` only       | All branches in `origin`              | Fetch all branches from `origin`                      |

This shows commits that exist on `origin/main` but not on your local `main`.| `git fetch origin main`| `origin` only       | Only `origin/main`                    | Quickly fetch updates just for the `main` branch      |



## Fetch vs Pull- **`git fetch`**: Good if you have multiple remotes or want to update all remote branches.

- **`git fetch origin`**: Focuses on fetching all branches from `origin` only.

People confuse these. They're different.- **`git fetch origin main`**: Ideal if you care only about the `main` branch.



```mermaid---

graph TB

    subgraph "git fetch origin"## **5. Advanced Options**

        A1[Remote] -->|Download info| B1[.git updates]

        B1 -.->|Files untouched| C1[Working Dir]### **5.1 Fetch All Remotes**

    end```bash

    git fetch --all

    subgraph "git pull origin main"```

        A2[Remote] -->|Download + Merge| B2[.git updates]- Retrieves updates from **every remote** (e.g., `origin`, `upstream`).

        B2 -->|Files changed| C2[Working Dir]

    end### **5.2 Prune Deleted Branches**

    ```bash

    style B1 fill:#e8f5e9git fetch --prune

    style C1 fill:#f0f0f0```

    style B2 fill:#fff4e1- Removes remote-tracking branches in your local repo that **no longer exist** on the remote.

    style C2 fill:#ffcdd2- Combine both:

```  ```bash

  git fetch --all --prune

| Command | What it does | Safe? |  ```

|---------|--------------|-------|

| `git fetch origin` | Downloads info. Doesn't change your files. | ✅ Always safe |### **5.3 Verbose Output**

| `git pull origin main` | Fetches AND merges. Changes your files. | ⚠️ Can cause conflicts |```bash

git fetch --verbose

!!! tip "Best Practice"```

    Fetch first. Review changes. Then decide to pull or merge.- Shows detailed information about what’s being fetched.



    ```bash---

    git fetch origin

    git log main..origin/main --oneline## **6. Viewing Fetched Changes**

    # Look at what changedAfter fetching, compare your local branch to the fetched remote branch:

    git pull origin main

    ``````bash

git log main..origin/main --oneline

## Common Workflow```

- Shows commits that exist in `origin/main` but not in your local `main`.

Here's how you'll actually use fetch:

Or use `git diff`:

### Morning Routine```bash

git diff main origin/main

You start work. Check what changed overnight.```

- Shows exact code differences.

```bash

# Fetch latest info---

git fetch origin

## **7. Applying Fetched Changes**

# Check if main movedSince fetching alone doesn’t modify your local branches, you can:

git log main..origin/main --oneline

```1. **Pull** (merge automatically)

   ```bash

If there are new commits, update your local `main`:   git pull origin main

   ```

```bash2. **Merge** (manual merge)

git checkout main   ```bash

git pull origin main   git merge origin/main

```   ```

3. **Rebase**

Now you're synced. Create your feature branch from updated `main`.   ```bash

   git rebase origin/main

### Before Creating a Pull Request   ```



Your feature is done. But the team kept working. Main moved ahead.---



```bash## **8. Example Scenarios**

# Check what changed on main

git fetch origin### **8.1 Quick Check Before Pulling**

git log origin/main --oneline -51. **Fetch** the latest changes:

```   ```bash

   git fetch origin

If main has new commits, update your branch:   ```

2. **Inspect** what’s new:

```bash   ```bash

git checkout main   git log main..origin/main --oneline

git pull origin main   ```

git checkout feature/your-feature3. **Pull** if needed:

git merge main   ```bash

```   git pull origin main

   ```

Now your branch includes the latest from main. No surprises during code review.

### **8.2 Multiple Remotes**

### Checking on Teammate's Branch1. You fork a repository, so you have:

   - `origin` → Your fork.

Your teammate is working on `feature/payment`. You want to see their latest work.   - `upstream` → The original repo.

2. Fetch updates from both:

```bash   ```bash

# Fetch their branch   git fetch origin

git fetch origin feature/payment   git fetch upstream

   ```

# Check what they did3. Merge changes from `upstream` if necessary.

git log origin/feature/payment --oneline -10

### **8.3 Checking a Single Branch**

# Or see the actual code```bash

git diff main origin/feature/paymentgit fetch origin feature/awesome-feature
  • Fetches only the specified branch from origin.

You didn't checkout anything. Didn't change your working directory. Just reviewed their work.


Quick Command Reference

9. Key Takeaways

| Task | Command |- git fetch is a safe way to update your local knowledge of remote branches without modifying your local branches.

|------|---------|- Use git fetch --all --prune to keep your local copy of remote branches clean.

| Fetch all branches from origin | git fetch origin |- Use git fetch origin <branch> if you only need updates for a specific branch.

| Fetch specific branch | git fetch origin branch-name |- If you want those fetched changes in your local branch, merge, pull, or rebase.

| Fetch from all remotes | git fetch --all |

| Check what's new on main | git log main..origin/main --oneline |---

| See code differences | git diff main origin/main |

| Check if you're behind | git status |Happy Fetching! 🚀

Advanced Usage

Everything below is optional. But useful for complex scenarios.

Fetch Commands Explained

Different Ways to Fetch

Fetch everything

git fetch

Fetches from all configured remotes. If you only have origin, same as git fetch origin.

Fetch from specific remote

git fetch origin

Fetches all branches from origin. This is what you'll use 90% of the time.

Fetch specific branch

git fetch origin main

Only fetches the main branch. Faster if you only care about one branch.

Fetch all remotes

git fetch --all

If you have multiple remotes (like origin and upstream), this fetches from all of them.

Comparison:

Command Fetches From Updates
git fetch All remotes All branches
git fetch origin origin only All origin branches
git fetch origin main origin only Only main
git fetch --all All remotes All branches from all remotes

Cleaning Up Stale Branches

Using --prune to Remove Dead Branches

Your teammate deleted feature/old-work on remote. But your local git still tracks origin/feature/old-work.

See stale branches:

git branch -r

You'll see branches that don't exist on remote anymore.

Clean them up:

git fetch --prune

Or automatically prune every time:

git config --global fetch.prune true

Now every git fetch will remove stale branches.

Combined command:

git fetch --all --prune

Fetches from all remotes AND removes dead branches. Clean and efficient.

Working with Multiple Remotes

Fetching from Upstream (Forked Repos)

You forked a repository. You have two remotes:

  • origin → Your fork on GitHub
  • upstream → The original repository

Setup upstream (one time):

git remote add upstream https://github.com/original-owner/repo.git

Check your remotes:

git remote -v

Output:

origin    https://github.com/you/repo.git (fetch)
origin    https://github.com/you/repo.git (push)
upstream  https://github.com/original-owner/repo.git (fetch)
upstream  https://github.com/original-owner/repo.git (push)

Fetch from both:

git fetch origin
git fetch upstream

Or fetch from all at once:

git fetch --all

Update your fork with upstream changes:

# Fetch latest from upstream
git fetch upstream

# Checkout your main
git checkout main

# Merge upstream's main
git merge upstream/main

# Push to your fork
git push origin main

Now your fork is synced with the original repo.

Viewing What Changed

Inspecting Fetched Changes

You fetched. Now what? How do you see what changed?

See commits on origin/main that you don't have:

git log main..origin/main --oneline

Output:

a3f2c1d Fix authentication bug
b7e9f3a Add password validation
c4d8e2f Update README

See commits you have that origin doesn't:

git log origin/main..main --oneline

If this shows commits, you're ahead of remote.

See code differences:

git diff main origin/main

Shows actual line-by-line changes.

See only which files changed:

git diff --name-only main origin/main

Output:

auth.js
README.md
package.json

See commit graph:

git log --oneline --graph main origin/main

Visual representation of branch history.

Fetching Tags

Working with Tags

Tags are version markers. Like v1.0.0, v2.0.0.

Fetch all tags:

git fetch --tags

List all tags:

git tag

Checkout a specific version:

git checkout v1.0.0

You're now in "detached HEAD" state, viewing the code at version 1.0.0.

Get back to your branch:

git checkout main

Verbose Output

See What's Happening During Fetch

Want to see exactly what git is doing?

git fetch --verbose

Or even more detail:

git fetch --verbose --progress

Output:

remote: Counting objects: 25, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 25 (delta 10), reused 20 (delta 5)
Unpacking objects: 100% (25/25), done.
From https://github.com/username/repo
   a3f2c1d..b7e9f3a  main       -> origin/main
   c4d8e2f..f1a5b9c  develop    -> origin/develop

Real-World Scenarios

Scenario 1: Before Starting Work

You're about to start a new feature. First, sync your knowledge.

# Fetch latest info
git fetch origin

# Check if main moved
git log main..origin/main --oneline

If main has new commits:

git checkout main
git pull origin main
git checkout -b feature/new-work

Now you're branching off the latest code.

Scenario 2: During Code Review

Someone requested changes on your PR. Meanwhile, main moved ahead.

# Fetch latest
git fetch origin

# Update main
git checkout main
git pull origin main

# Update your feature branch
git checkout feature/your-work
git merge main

# Push updated branch
git push origin feature/your-work

Scenario 3: Checking Teammate's Work

Your teammate asked you to review their branch before they open a PR.

# Fetch their branch
git fetch origin feature/their-work

# Compare to main
git log main..origin/feature/their-work --oneline

# See the code diff
git diff main origin/feature/their-work

You reviewed without touching your working directory.

Common Mistakes

Don't Confuse Fetch and Pull

Wrong:

# Thinking this will update your files
git fetch origin main
# Your files didn't change!

Right:

# Fetch to check
git fetch origin main
# Then pull to apply
git pull origin main

Remote Branches Are Read-Only

You can't checkout and work on origin/main directly.

Wrong:

git checkout origin/main
# You're in detached HEAD!

Right:

git checkout main
git pull origin main
# Now you're on your local main

Best Practices

Do This

  • Fetch before starting work
  • Fetch before creating a PR
  • Use git fetch origin as your default
  • Review changes before pulling
  • Set up auto-prune: git config --global fetch.prune true

Pro Workflow

Make this your morning routine:

git fetch --all --prune
git status
git log main..origin/main --oneline

You'll know exactly what changed overnight.

What's Next?

You understand fetch. You know how to check for changes without risking your local work.

Next up: git pull, git merge, and git rebase. How to actually apply those fetched changes.

But fetch is your safety net. Always fetch first. Review. Then act.