0
0
GitHow-ToBeginner · 3 min read

How to Clone a Specific Branch in Git Quickly

Use the git clone --branch <branch-name> <repository-url> command to clone only the specific branch you want. This downloads the chosen branch instead of the default branch.
📐

Syntax

The command to clone a specific branch uses the --branch option followed by the branch name and the repository URL.

  • git clone: The command to copy a repository.
  • --branch <branch-name>: Specifies which branch to clone.
  • <repository-url>: The web address of the Git repository.
bash
git clone --branch <branch-name> <repository-url>
💻

Example

This example clones the branch named feature-login from a GitHub repository.

bash
git clone --branch feature-login https://github.com/example/repo.git
Output
Cloning into 'repo'... remote: Enumerating objects: 42, done. remote: Counting objects: 100% (42/42), done. remote: Compressing objects: 100% (30/30), done. Receiving objects: 100% (42/42), 12.34 KiB | 1.23 MiB/s, done. Resolving deltas: 100% (10/10), done.
⚠️

Common Pitfalls

One common mistake is forgetting to use the --branch option, which clones the default branch instead of the desired one. Another is misspelling the branch name, causing Git to clone the default branch without error.

Also, cloning a branch does not limit the repository to that branch only; all branches are still available locally unless you use additional options.

bash
Wrong:
git clone https://github.com/example/repo.git

Right:
git clone --branch feature-login https://github.com/example/repo.git
📊

Quick Reference

Remember these tips when cloning a specific branch:

  • Use --branch to specify the branch.
  • Check the branch name spelling carefully.
  • Cloning a branch still downloads the full repo history.
  • Use --single-branch to avoid downloading other branches.

Key Takeaways

Use git clone --branch <branch-name> <repository-url> to clone a specific branch.
Always verify the branch name to avoid cloning the default branch by mistake.
Cloning a branch downloads the full repository history unless --single-branch is added.
Without --branch, Git clones the default branch automatically.
Use --single-branch with --branch to limit download to that branch only.