Git Clone Branch – How to Clone a Specific Branch (2024)

Unlike older centralized version control systems such as SVN and CVS, Git is distributed. Every developer has the full history and control of their code locally or remotely. They can also access or manipulate several parts of the code as they deem fit from different locations.

Since Linus Torvalds (the famous creator of the Linux operating system kernel) created Git in 2005 for Linux kernel development, it has become the most widely used modern version control system in the world.

In this article, I'll introduce you to the Git clone and Git branch workflows and I'll show you how you can clone a specific branch based on your needs. Let's begin! ?

Prerequisites

  • Basic knowledge of the terminal
  • Ability to type commands in the terminal
  • Git installed (I'll still show you how)
  • A GitHub account
  • A smile on your face (Put up that smile friend ?)

Quick Introduction to Git and GitHub

According to Wikipedia,

Git is a distributed version control system designed to track changes to a project (code) in software development. It is intended to enforce coordination, collaboration, speed, and efficiency among developers.

GitHub, on the other hand, is a web-based hosting service for version control using Git. It offers all of the distributed version control and source code management functionality of Git as well as adding more features for computer code.

How to Install Git on Windows

Download and install the latest Git for Windows Installer here.

How to Install Git on Linux

Here are the commands based on your Linux distro:

Debian or Ubuntu

sudo apt-get updatesudo apt-get install git

Fedora

sudo dnf install git

CentOS

sudo yum install git

Arch Linux

sudo pacman -Sy git

Gentoo

sudo emerge --ask --verbose dev-vcs/git

How to Install Git on a Mac

Download and install the latest Git for Mac installer here.

Or you can type this command:

brew install git

Now that we've got Git installed, let's move on to the tutorial.

Introduction to Git Clone

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

You can then clone this repository to your local machine and have all the files and branches locally (I'll explain more about branches soon).

Git Clone Branch – How to Clone a Specific Branch (1)

For example, you can clone freeCodeCamp's repository with SSH like so:

git clone git@github.com:freeCodeCamp/freeCodeCamp.git

Introduction to Git Branches

When working on a project, you will likely have different features. And multiple contributors will be working on this project and its features.

Branches allow you to create a "playground" with the same files in the master branch. You can use this branch to build independent features, test new features, make breaking changes, create fixes, write docs or try out ideas without breaking or affecting the production code. When you're done, you merge the branch into the production master branch.

Branching is a core concept in Git which is also used in GitHub to manage workflows of different versions of one project. The master branch is always the default branch in a repository that is most often considered "production and deployable code". New branches like passwordless-auth or refactor-signup-ux can be created from the master branch.

Git Clone Branch – How to Clone a Specific Branch (2)

How to Clone Git Branches

While you can clone repositories with the git clone command, keep in mind that this clones the branch and the remote HEAD. This is usually master by default and includes all other branches in the repository.

So when you clone a repository, you clone the master and all other branches. This means you will have to checkout another branch yourself.

Let's say your task on a project is to work on a feature to add passwordless authentication to a user dashboard. And this feature is in the passwordless-auth branch.

You really don't need the master branch since your "feature branch" will be merged into master afterward. How then do you clone this passwordless-auth branch without fetching all other branches with "a bunch of files you don't need"?

I created this sample repository to explain this. This repository holds a simple blog built with Nextjs and has four dummy branches:

  • master
  • dev
  • staging
  • passwordless-auth

In Nextjs, any file inside the folder pages/api is mapped to the /api/* path and will be treated as an API endpoint instead of a page. In our repository, I have created different dummy APIs in this directory to make each branch different.

The master branch holds the file pages/api/hello.js while passwordless-auth holds the file pages/api/auth.js. Each file just returns a dummy text response. See master's hello API response here (with a special message for you ?).

Let's clone the repository:

git clone git@github.com:BolajiAyodeji/nextjs-blog.git

This gives us access to all branches in this repository and you can easily toggle between each to see each version and its files.

git branch -a
Git Clone Branch – How to Clone a Specific Branch (3)

Wondering where the remotes/origin/.. branches came from?

When you clone a repository, you pull data from a repository on the internet or an internal server known as the remote. The word origin is an alias created by your Git to replace the remote URL (you can change or specify another alias if you want).

These remotes/origin/.. branches point you back to the origin repository you cloned from the internet so you can still perform pull/push from the origin.

Git Clone Branch – How to Clone a Specific Branch (4)

So when you clone master onto your machine, remotes/origin/master is the original master branch on the internet, and master is on your local machine. So you will pull/push from and to the remotes/origin/master.

In summary Remote is the URL that points you to the repository on the internet while Origin is an alias for this remote URL.

Git Clone Branch – How to Clone a Specific Branch (5)

How to Clone a Specific Branch

Now let's clone a specific branch from our demo repository. There are two ways to clone a specific branch. You can either:

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

Option One

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

or

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


With this, 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 you still fetched all files from each branch. This might not be what you want right? ?

Let's test it:

 git clone -b passwordless-auth git@github.com:BolajiAyodeji/nextjs-blog.git

This automatically configures passwordless-auth as the local branch but still tracks other branches.

Git Clone Branch – How to Clone a Specific Branch (6)
Git Clone Branch – How to Clone a Specific Branch (7)

Option Two

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

or

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

This performs the same action as option one, except that the --single-branch option was introduced in Git version 1.7.10 and later. It allows you to only fetch files from the specified branch without fetching other branches.

Let's test it:

git clone -b passwordless-auth --single-branch git@github.com:BolajiAyodeji/nextjs-blog.git

This automatically configures passwordless-auth as the local branch and only tracks this branch.

Git Clone Branch – How to Clone a Specific Branch (8)
Git Clone Branch – How to Clone a Specific Branch (9)

If you run cd pages/api you'll find the auth.js file in the passwordless-auth branch as expected from the previous setup.

Conclusion

You might be running out of internet or storage space but you need to work on a task in a specific branch. Or you might want to clone a specific branch with limited files for various reasons. Fortunately, Git provides you the flexibility to do this. Flex your muscles and try it out, there's much more "Git" to learn.

One at a time, yeah? ✌?

Git Clone Branch – How to Clone a Specific Branch (2024)

FAQs

How to clone specific branch only? ›

Command to clone a specific Git branch
  1. The –single-branch (two dashes) flag limits the clone to only one branch.
  2. The –branch (two dashes) flag specifies which specific Git branch to clone.
Sep 2, 2023

How to clone a particular branch in git desktop? ›

From the list of repositories, click the repository you want to clone. To select the local directory into which you want to clone the repository, next to the "Local Path" field, click Choose... and navigate to the directory. At the bottom of the "Clone a Repository" window, click Clone.

How to clone only one branch from GitLab? ›

There are two ways to clone specific Git branches in GitLab.
  1. Method A — Clone your repository, fetch all repository branches, and immediately checkout to the specific branch.
  2. Method B — Clone your repository and only fetch a single branch.

How to git fetch from a specific branch? ›

How to Git Pull from a Specific Branch
  1. Step 1: Switch to the desired branch.
  2. Step 2: Fetch the latest changes.
  3. Step 3: Pull the changes from the specific branch.
  4. Alternative Method: Pull with rebase.
Oct 27, 2023

How to checkout a specific branch in git? ›

Using Git to checkout a branch on the command line
  1. Change to the root of the local repository. $ cd <repo_name>
  2. List all your branches: $ git branch -a. ...
  3. Checkout the branch you want to use. $ git checkout <feature_branch>
  4. Confirm you are now working on that branch: $ git branch.

When you clone a repo do you clone all branches? ›

Realistically, cloning a repository fetches all the branches but only checks out the default branch, usually master or main, leaving the rest in a remote tracking state.

How to pull a specific branch in GitLab? ›

1- When you do git checkout -b test it creates copy of your current branch(in this case 'dev'). 2- git pull will only sync your changes between remote and local. If you upload the branch and try to pull, it does not work because your local and remote changes will be synchronized.

What is the usage of git clone branch command? ›

git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. The original repository can be located on the local filesystem or on remote machine accessible supported protocols. The git clone command copies an existing Git repository.

How do I clone a particular branch from Bitbucket? ›

Clone and make a change on a new branch
  1. From the repository, click the Clone button in the top right. Bitbucket displays the Clone this repository dialog. ...
  2. Copy the clone command.
  3. From a terminal window, change into the local directory where you want to clone your repository. $ cd ~/<path_to_directory>

What is the difference between git fetch and clone? ›

Git clone creates a copy of the code in your local machine. Git fetch , fetches code from git to your local repository but will not update it. Once Git merge is executed the code is updated.

How do I pull a specific branch from another git repository? ›

Steps to Pull from a Specific Branch
  1. Step 1: Check Current Branch. First, let's ensure that you are currently on the branch from which you want to pull changes. ...
  2. Step 2: Switch to the Target Branch. ...
  3. Step 3: Pull Changes from the Specific Branch. ...
  4. Step 4: Resolve any Merge Conflicts (if any) ...
  5. Step 5: Verify Changes.
May 27, 2024

What is the difference between pull and fetch? ›

Difference between Git fetch and pull. The key difference between git fetch and pull is that git pull copies changes from a remote repository directly into your working directory, while git fetch does not. The git fetch command only copies changes into your local Git repo.

How to clone a specific branch in Azure DevOps? ›

From your web browser, open the team project for your organization and select Repos > Files. Select Clone in the upper-right corner of the Code window and copy the URL. Git downloads a copy of the code, including all commits, and branches from the repo, into a new folder for you to work with.

How to clone only specific files from git? ›

Method 1: sparse checkout
  1. Initialize a new Git repository: git init <repo-name> ...
  2. Add the remote repository: git remote add origin <repository-url> ...
  3. Enable sparse checkout: ...
  4. Create a sparse-checkout file that specifies which files to check out: ...
  5. Fetch the data and checkout the specific file:

How to clone a particular branch from Bitbucket? ›

Clone and make a change on a new branch
  1. From the repository, click the Clone button in the top right. Bitbucket displays the Clone this repository dialog. ...
  2. Copy the clone command.
  3. From a terminal window, change into the local directory where you want to clone your repository. $ cd ~/<path_to_directory>

References

Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6264

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.