Sling Academy
Home/DevOps/Git: How to clone only a specific remote branch

Git: How to clone only a specific remote branch

Last updated: January 27, 2024

When working with Git, the version control system used by millions of developers, you might encounter a situation where you need to clone only a specific branch from a repository. This becomes very useful when dealing with large repositories where cloning the entirety of the project would be time-consuming and unnecessary. This tutorial will guide you through the steps needed to achieve this, going from basic to more advanced use-cases, including examples and their expected outputs.

Understanding Git Clone

Before diving into how to clone a specific branch, let’s understand what the git clone command does. The git clone command is used to copy a repository from a remote location to your local machine. By default, it copies all the branches in the repository, checks out the default branch (usually master or main), and sets the origin repository as a remote so you can fetch from it and push to it later on.

Cloning a Specific Branch Using –branch

To clone a specific branch, you can use the --branch option followed by the name of the branch you want to clone. Below is the syntax:

git clone --branch  

For example, if you want to clone the development branch from a repository at https://github.com/user/repo.git, you would use:

git clone --branch development https://github.com/user/repo.git

This command clones the development branch,and if successful, you should see the output:

Cloning into 'repo'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 10 (delta 2), reused 10 (delta 2)
Unpacking objects: 100% (10/10), done.

Further Controlling Clone Behavior

Sometimes, you may want to further limit what gets cloned. For instance, if you’re only interested in the most recent commit of a specific branch, you can combine the --single-branch and the --depth 1 options:

git clone --branch  --single-branch --depth 1 

Using the --single-branch option tells Git to clone only the history of the specified branch. Combining it with --depth 1 creates a shallow clone with a history truncated to the last commit. This can significantly reduce the time and bandwidth needed for the clone operation.

Exploring Advanced Clone Scenarios

In more complex scenarios, for instance, when the branch you want to clone does not exist on the default remote named ‘origin’, you would first need to add that remote and then proceed with the cloning:

git remote add  
git clone --branch  /repo.git

Alternatively, if you’ve already cloned the repository and need to get only the specific branch’s latest changes, you might prefer fetching and checking out the branch:

git fetch origin 
git checkout 

This will update your local branch with the remote branch’s latest commits.

Conclusion

Cloning only a specific branch in Git can be very straightforward if you know the right commands. By using the --branch, --single-branch, and --depth options in combination, you can effectively manage and optimize your cloning tasks. Remember the essentials discussed here, and you’ll be able to tailor your Git workflow to better suit the needs of your project and team.

Next Article: Git: How to ignore file mode (chmod) changes

Previous Article: Fixing Git Error: Permission denied (publickey) – 4 solutions

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide