0
0
GitHow-ToBeginner · 3 min read

How to Fetch a Specific Branch in Git Quickly

Use git fetch origin branch-name to fetch only the specific branch from the remote repository. This downloads the branch updates without affecting your current working branch.
📐

Syntax

The basic syntax to fetch a specific branch from a remote repository is:

  • git fetch <remote> <branch-name>

Here, <remote> is usually origin, the default name for your remote repository. <branch-name> is the name of the branch you want to fetch.

bash
git fetch origin branch-name
💻

Example

This example shows how to fetch the branch named feature-login from the remote origin. It updates your local copy of that branch without switching your current branch.

bash
git fetch origin feature-login
Output
From https://github.com/user/repo * branch feature-login -> FETCH_HEAD
⚠️

Common Pitfalls

One common mistake is running git fetch without specifying the branch, which fetches all branches and can take longer. Another is expecting git fetch to switch your working branch; it only updates remote tracking branches.

To switch to the fetched branch, you must run git checkout branch-name or git switch branch-name after fetching.

bash
git fetch origin
# fetches all branches

git fetch origin feature-login
# fetches only 'feature-login' branch

git checkout feature-login
# switches to the fetched branch
📊

Quick Reference

Remember these quick tips:

  • Fetch specific branch: git fetch origin branch-name
  • Fetch all branches: git fetch origin
  • Switch to branch: git checkout branch-name or git switch branch-name

Key Takeaways

Use git fetch origin branch-name to download only the specific branch.
Fetching does not change your current branch; you must checkout to switch.
Fetching all branches takes longer and is unnecessary if you want just one branch.
After fetching, use git checkout or git switch to work on the branch.
Always specify the remote and branch name to avoid confusion.