git
download branch
git tutorial
version control
branch management

How to download a branch from GitHub with Git

Codemia Knowledge Hub ·

To download a branch, clone it directly with git clone --branch <name> <url>, or in a repository you already have, run git fetch origin and then git switch --track origin/<name>. Git does not download a branch as a separate file: it fetches the branch's commits and references from the remote, and you check out a local branch that tracks the remote one.

How remote branches work

A remote branch such as origin/feature-x is a tracking reference stored in your local repository. To work on its commits you create a local branch, such as feature-x. The flow is always:

  • fetch branch updates from the remote
  • create or update the local tracking branch
  • switch to that local branch

Cloning one branch

If you only need one branch to start, clone straight to it:

bash
1# Clone and switch to a specific branch
2git clone --branch feature-x https://github.com/example/project.git
3cd project
4
5# Verify current branch
6git branch --show-current

This clones the repository and checks out feature-x. Depending on remote settings, other branch refs may still be fetched later.

Fetching and tracking a branch in an existing repository

bash
1cd project
2
3# Update remote references
4git fetch origin
5
6# Create local branch from remote branch and switch to it
7git switch --track origin/feature-x
8
9# Alternative for older Git
10git checkout -b feature-x origin/feature-x

After this, git pull and git push use the tracked remote branch by default.

Listing the remote branches

If you are not sure of the branch name, list the branches first:

bash
git fetch origin
git branch -r

Or ask the remote directly:

bash
git ls-remote --heads origin

This avoids typos and outdated assumptions about branch names.

Updating all branches at once

To refresh references for every branch without switching:

bash
git fetch --all --prune

--prune removes remote-tracking refs for branches that no longer exist on the remote, which keeps branch lists clean.

Fetching a branch without switching to it

To get a branch's data locally while staying on your current branch:

bash
git fetch origin feature-x

origin/feature-x is now up to date, and you can inspect its commits:

bash
git log --oneline --decorate origin/feature-x -n 10

This is handy in release workflows where you review a branch before checking it out.

Authentication errors on private repositories

If the fetch fails with an authentication error, check the remote URL and your credentials:

bash
git remote -v

Usual fixes:

  • use the SSH URL if SSH keys are set up
  • use HTTPS with a credential helper
  • confirm your account can access the repository or branch

Never put plaintext credentials in remote URLs inside committed scripts.

Keeping the local branch in sync

bash
git switch feature-x
git pull --ff-only

--ff-only avoids implicit merge commits during routine syncing and keeps history predictable.

A full example session

bash
1# One-time setup
2cd project
3git fetch origin
4git switch --track origin/feature-x
5
6# Daily work
7git pull --ff-only
8# edit files
9git add .
10git commit -m "Implement feature update"
11git push

If local and remote diverge, rebase or merge deliberately instead of relying on the default behavior.

When the branch is not found

If origin/feature-x is missing:

  • run git fetch origin --prune
  • check the exact spelling and case of the branch name
  • confirm the branch exists in the hosting UI
  • confirm you have access to it

If the branch exists remotely but still does not show up locally, check whether the fetch refspec in .git/config is restricted.

Mistakes to avoid

  • Expecting git pull to create a local branch that does not exist yet.
  • Running git checkout feature-x before fetching the remote refs.
  • Forgetting --track, so pushing fails because there is no upstream.
  • Mixing up local branch names and remote-tracking names.
  • Working on stale refs without running git fetch first.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions