Git: How to clone only a specific remote branch

Updated: January 27, 2024 By: Guest Contributor Post a comment

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.