The Ops Community ⚙️

Sarah Lean
Sarah Lean

Posted on • Originally published at

Git Clone Branch – How to Clone a Specific Branch

Git Clone Branch – How to Clone a Specific Branch

Since its inception in 2005 by Linus Torvalds, renowned for creating the Linux operating system kernel, Git has emerged as the predominant modern version control system globally.

Within this article, I aim to show you the workflows of Git cloning and branching. Furthermore, I'll demonstrate how you can clone a specific branch tailored to your requirements. Let's dive in!


A quick guide to Git and GitHub

Git stands as a distributed version control system meticulously crafted to monitor the evolution of a project, particularly in the realm of software development. Its purpose is crystal clear: to foster seamless coordination, amplify collaboration, and infuse projects with a burst of speed and efficiency, ultimately empowering developers to conquer new frontiers with ease.

In contrast, GitHub emerges as a dynamic web-based hosting service tailored for version control utilizing Git. It not only encapsulates all the elements of distributed version control and source code management inherent in Git but also elevates the experience by introducing a plethora of additional features specifically curated for engineers.

Introduction to Git Clone

Git allows you to manage and version control your project(s) in what is called a repository. This repository can be stored on a web-based hosting service, like GitHub.

You can clone this repository to your local machines and have all the files and branches locally to work on.

Git Clone Branch – How to Clone a Specific Branch
GitHub repository

For example, you can clone this repository that I have using the following command:

git clone

Enter fullscreen mode Exit fullscreen mode

Introduction to Git branches

In any project, you're likely to juggle various features handled by multiple contributors. Branches serve as invaluable "playgrounds" within the project's framework.

They provide a haven where you can tinker with the same files found in the main branch.

Here, you're free to craft independent features, test novel ideas, implement daring changes, address issues, draft documentation, or experiment without jeopardising the stability of the production code. Once your work is polished, merging the branch back into the main branch seamlessly integrates your enhancements.

Branching stands as a cornerstone in Git's philosophy, a concept seamlessly integrated into GitHub to streamline the management of different project versions.

Within this ecosystem, the main branch reigns as the default, housing what's typically deemed as "production-ready and deployable code."

From this sturdy foundation, new branches like new-feature or documentation-update can sprout, each representing a unique avenue of development stemming from the main branch.

How to clone Git branches

When you embark on cloning repositories using the trusty git clone command, remember that it not only replicates the branch but also grabs the remote HEAD. By default, this usually corresponds to the main branch and encompasses all other branches within the repository.

Therefore, when you initiate a repository clone, you're essentially fetching the main branch alongside all its companions.

You can list all the branches you have on your local machines using the command

git branch -a

Enter fullscreen mode Exit fullscreen mode

You can switch to another branch using the command:

git checkout new-feature

Enter fullscreen mode Exit fullscreen mode

Now if you wanted to only clone a specific branch there are two ways you can use, either:

  • Clone the repository, fetch all branches, and checkout to a specific branch immediately.
  • Clone the repository and fetch only a single branch.

The command you can use to fetch only a single branch is:

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

Enter fullscreen mode Exit fullscreen mode

If I wanted to clone a branch from my example repository in GitHub my command would be:

git clone --branch auth-feature

Enter fullscreen mode Exit fullscreen mode


By the way, if you're interested in mastering Git and want to embark on a structured learning journey, why not join my 14 days of Git learning course? It's packed with insightful lessons, hands-on exercises, and practical tips to help you become a Git ninja in no time. So, what are you waiting for? Let's embark on this Git adventure together!

Top comments (0)