Skip to content

Understanding the git fetch Command

The git fetch command helps you see what’s new in a remote repository without modifying any of your local branches. It’s like checking the mail without opening any letters.


1. What Does git fetch Do?

  • 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.
  • It’s safe: you can fetch as often as you like without worrying about overwriting local changes.

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.


2. Common Use Cases

  1. Check for new commits on the remote before merging changes locally.
  2. Review differences between your local branch and the remote branch.
  3. Fetch updates from multiple remotes in complex setups (e.g., origin + upstream).

3. Basic Usage

3.1 Fetch from the Default Remote

git fetch
- Fetches updates from all configured remotes (commonly origin). - If you have only one remote, it’s effectively the same as git fetch origin.

3.2 Fetch from a Specific Remote

git fetch origin
- Fetches the latest updates only from the remote named origin. - Updates all branches in origin (e.g., origin/main, origin/feature-xyz). - Does not update local branches automatically.

3.3 Fetch Only a Specific Branch

git fetch origin main
- Fetches only the main branch from the remote named origin. - Updates only origin/main, without touching other branches.


4. Differences at a Glance

Command Fetches Updates From Updates Which Branches? Use Case
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
git fetch origin main origin only Only origin/main Quickly fetch updates just for the main branch
  • 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.
  • git fetch origin main: Ideal if you care only about the main branch.

5. Advanced Options

5.1 Fetch All Remotes

git fetch --all
- Retrieves updates from every remote (e.g., origin, upstream).

5.2 Prune Deleted Branches

git fetch --prune
- Removes remote-tracking branches in your local repo that no longer exist on the remote. - Combine both:
git fetch --all --prune

5.3 Verbose Output

git fetch --verbose
- Shows detailed information about what’s being fetched.


6. Viewing Fetched Changes

After fetching, compare your local branch to the fetched remote branch:

git log main..origin/main --oneline
- Shows commits that exist in origin/main but not in your local main.

Or use git diff:

git diff main origin/main
- Shows exact code differences.


7. Applying Fetched Changes

Since fetching alone doesn’t modify your local branches, you can:

  1. Pull (merge automatically)
    git pull origin main
    
  2. Merge (manual merge)
    git merge origin/main
    
  3. Rebase
    git rebase origin/main
    

8. Example Scenarios

8.1 Quick Check Before Pulling

  1. Fetch the latest changes:
    git fetch origin
    
  2. Inspect what’s new:
    git log main..origin/main --oneline
    
  3. Pull if needed:
    git pull origin main
    

8.2 Multiple Remotes

  1. You fork a repository, so you have:
  2. origin → Your fork.
  3. upstream → The original repo.
  4. Fetch updates from both:
    git fetch origin
    git fetch upstream
    
  5. Merge changes from upstream if necessary.

8.3 Checking a Single Branch

git fetch origin feature/awesome-feature
- Fetches only the specified branch from origin.


9. Key Takeaways

  • 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.
  • Use git fetch origin <branch> if you only need updates for a specific branch.
  • If you want those fetched changes in your local branch, merge, pull, or rebase.

Happy Fetching! 🚀