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:
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
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:
Or ask the remote directly:
This avoids typos and outdated assumptions about branch names.
Updating all branches at once
To refresh references for every branch without switching:
--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:
origin/feature-x is now up to date, and you can inspect its commits:
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:
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
--ff-only avoids implicit merge commits during routine syncing and keeps history predictable.
A full example session
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 pullto create a local branch that does not exist yet. - Running
git checkout feature-xbefore 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 fetchfirst.
Related reading
- How to draw checkbox or tick mark in GitHub Markdown table?
- How to exclude certain directories/files from a Git grep search
- How to exclude file only from root folder in Git
- How to execute a command right after a fetch or pull command in git?
- How to exit a 'git status' list in a terminal?
- How to exit git log or git diff
- How to export revision history from mercurial or git to cvs?
- How to fast-forward a branch to head
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.