How to clone a specific branch with git

April 8th, 20234 mins read

How to clone a specific branch with git

Git is a popular version control system used in software development. It enables developers to track changes in their codebase, collaborate with others, and revert to previous versions of their code when necessary.

Cloning is an essential feature of Git that allows developers to make local copies of a repository. In this article, you will learn how to clone a specific branch with Git.

Understanding Git Branching

Branching is a feature in Git that allows developers to create separate versions of their codebase.

This means, developers can create a new branch from an existing one, work on new features or bug fixes in the new branch, and merge changes back into the original branch when they are ready.

There are different types of branches, including the main branch (usually called "master" or "main"), feature branches, release branches, and hotfix branches. Each branch serves a specific purpose and helps developers manage their codebase better.

On your Git hosting provider, you can access the various branches or using your terminal locally, you can verify the various branches by running the following command in your terminal:

git branch

Before exploring how to clone a specific Git branch, let’s refresh our knowledge of how to clone a repository.

How To Clone a Git Repository

To clone a repository in Git, you need to use the git clone command followed by the URL of the remote repository. The syntax is as follows:

git clone <remote-repo-url>

For example, if you want to clone a repository hosted on GitHub, you can use the following command:

git clone git@github.com:<username>/<repo-name>.git

Replace <username> with your GitHub username and <repo-name> with the name of the repository you want to clone.

This will create a local copy of the repository on your machine. By default, Git will clone the entire repository, including all branches and tags.

Let’s now learn how to clone a specific branch.

Cloning a Specific Branch in Git

Cloning a specific branch in Git involves creating a local copy of a remote repository's branch. This is useful when you want to work on a specific project branch without cloning the entire repository.

There are two ways to clone a specific branch.

Option 1: Clone and Checkout Immediately

This option will clone the repository, fetch all branches, and checkout to a specific branch immediately with just one command.

git clone --branch <branchname> <remote-repo-url>

The above command specifies the branch name that will be switched to immediately and the repository's URL. A shorter alternative for the command above is to use the -b alias for --branch, like so:

git clone -b <branchname> <remote-repo-url>

With this method, you fetch all the branches in the repository, checkout to the one you specified, and the specific branch becomes the configured local branch for git push and git pull.

But a downside to this option is that you still fetched all files from each branch. You might not want this if the repository has a large history.

Option 2: Clone the Repository and Fetch Only a Single Branch

To clone the repository and fetch only a single branch, use the following command:

git clone --branch <branchname> --single-branch <remote-repo-url>

The command above specifies the branch name and the repository's URL but one option makes it stand out from the previous command: --single-branch, which specifies that only files from the specified branch should be fetched.

An alternative for the command is to use the -b alias for --branch, like so:

git clone -b <branchname> --single-branch <remote-repo-url>

At this point, you may begin to ask which command is best. The answer is ensure you use the command that suits your need. If you wish to clone a repository with only files of a specific branch, then you'll have to use the second option.

Cloning a branch is different from checking out a branch. Checking out a branch means switching to a different branch in the local repository. This can be done after cloning the repository or anytime later. To check out a branch, use the following command:

git checkout <branchname>

Conclusion

Cloning a specific branch in Git is a useful feature that allows developers to work on specific features or bug fixes without cloning the entire repository.

By following the steps outlined in this article, you can easily clone a specific branch in Git and start working on it locally.

Remember that checking out a branch is different from cloning a branch, and you can switch between branches anytime using the git checkout command.

Happy coding!