Sub topic 8
Cloning a Specific Branch
This chapter assumes you can run git clone and know what a branch and a remote-tracking branch are. By default, a clone checks out the repository's default branch. Sometimes you want a different branch, or only one branch's history.
What a default clone gives you
A plain git clone downloads the history of every branch. It stores each one as a remote-tracking branch under origin/, and it creates one local branch, the remote's default branch (often main). You can see this with git branch -a.
git clone https://github.com/octocat/Hello-World.git
cd Hello-World
git branch -aThe output lists the local main branch and the remote-tracking branches under remotes/origin/.
Checking out a different branch with --branch
The --branch option (short form -b) makes the clone check out a branch you name instead of the default one. The clone still downloads every branch.
git clone --branch main https://github.com/octocat/Hello-World.git--branch also accepts a tag name. In that case you end up in a detached HEAD state, which means HEAD points at a commit rather than at a branch. That is fine for reading or building, but make a new branch before you commit.
Fetching only one branch with --single-branch
Add --single-branch to download only the history that leads to that branch. This saves time and disk space when the repository is large and you need just one line of development.
git clone --branch main --single-branch https://github.com/octocat/Hello-World.gitGit also configures the clone so that later fetches track only that branch. Other branches will not show up in git branch -a.
Switching to another branch later
With a full clone, every branch is already in your local object store. Switching creates a local branch that tracks the remote one.
git switch feature-xIf no local feature-x exists but origin/feature-x does, Git creates the local branch for you and sets it to track the remote branch. The older git checkout feature-x behaves the same way here.
In a single-branch clone, origin/feature-x is not known locally. You have to fetch it first by naming it explicitly.
git fetch origin feature-x:refs/remotes/origin/feature-x
git switch feature-xChoosing an approach
| Goal | Command option | Branches downloaded |
|---|---|---|
| Default branch, all history | none | All |
| Start on another branch | `--branch NAME` | All |
| Save time and space | `--branch NAME --single-branch` | One |
Common errors
Remote branch NAME not found in upstream origin: the branch name is misspelled, or it does not exist on the remote. Branch names are case-sensitive. List the remote's branches withgit ls-remote --heads URL.- Missing branches after a single-branch clone: this is expected. Fetch the branch you need, or reconfigure the remote with
git remote set-branches origin '*'and then rungit fetch origin. - Committing on a detached HEAD after cloning a tag: create a branch with
git switch -c my-branchbefore you commit.
Exercise
You ran git clone --branch develop --single-branch URL. Later you need the release branch. Why does git switch release fail, and what do you run first?
Show answer
A single-branch clone fetches and tracks only develop, so origin/release does not exist locally. Run git remote set-branches origin '*' and then git fetch origin to get all branches, or run git fetch origin release:refs/remotes/origin/release to get just that one. Then git switch release works.
Exercise
Which command lists the branches that exist on a remote without cloning it?
Show answer
git ls-remote --heads URL