How to Clone Git Branch (repository)?

How to Clone Git Branch?

The git clone command uses to copy the repository or branch, so in this tutorial, we will learn how to clone the git branch or repository, how to clone a specific branch from repository.

Github, GitLab, and BitBucket are version control systems to store your project’s data in a repository. Git allows you to manage these repositories and versions of your project.

By default, the git clone command copies all the branches from GitHub, GitLab, or BitBucket and moves them into our repository. But if you want to clone a specific branch then you have to use the –single-branch flag with the git clone command.

So, we will cover all the step-by-step guides and learn how to make a clone of the git branch and download it into our repository.

What is Git Cloning?

Basically cloning means, is the process of making the same identical thing from another thing. So in git wording, a clone is the process of copy all the content of the repository and make a new repository.

You don’t need a web interface to clone any repository or branch from the git version control system (GitHub, GitLab, BitBucket). You only need a git SSH or git HTTP address and you can download the content of the repository using the command line (CMD).

Clone a repository really all that means is, suppose we have a repository on GitHub GitLab or BitBucket and we want to copy the files to our computer.

There are three ways of doing this we can download the zip. We can download it with git using SSH or download with git using HTTP.

Three ways to download git repository:

  • Download the zip: Direct download the zip folder.
  • Git using SSH: Authenticate with GitHub using the SSH protocol and download the repository.
  • Git using HTTP: Access the git repository over http:// and https:// protocol and download it.

Using SSH and HTTP, you don’t need a web interface. You can download the git repository using the SSH and HTTP address from the command line.

If you want to download any specific brach or full repository or exclusive branch, then you just have to add the specific flag with the git clone command. Then git clone command will perform based on the flag and download the repository or branch for you.

Let’s jump into the main topic of this tutorial and see how to clone the git branch/repository with commands.

Before starting to clone a repository, you obviously need to have Git installed on your computer.

To check if Git is correctly installed on your system, you need to execute the following command.

If you see your git version it means, it is installed on your system, and now you are ready to make a clone of a git repository.

Clone a Git Repository Using git clone Command

To clone the git repository, use the git clone command and specify the repository SHH or HTTP address. This will clone the branch and the remote HEAD that is usually master by default.

So when you hit the above command it will clone a repository, which means you clone the master and all others branches.

Let’s say that if you want to clone a repository from GitHub or any other version control server then you need to execute the following command as an example. Don’t forget to change the git HTTP address with your git address.

This command will clone the full project repository master branch with all branches into a folder. The folder name by default will be the name of a repository. It will take time to clone depends on the size of the files. After the clone, you will get all the repositories in your project folder.

You can also clone your project repository into a specific folder by specifying the name of the folder in the command. Let’s see how you can clone the git repository into a specific folder

Clone a Git Repository into a Specific Folder

To clone the git repository into a specific folder, use the git clone command to specify the location of the folder at the end. See the following git clone command.

Let’s suppose the same thing that you want to clone a git repository from GitHub but into a specific folder called “my-project”. Then use the following git command example.

Now, let’s check if the cloning process is successfully done or not. So use the ls command to list all files and folders. If you see your repository folder in the list that means you have done successfully cloned.

Execute the following command to check the current branch cloned.

Ok, now.

In some cases, if you want to clone a specific branch from the master branch then you have to run the git command with a specific flag and also specifying the branch and repository name.

Let’s see how to clone a specific branch from the repository.

Git Clone a Specific Branch

In order to clone a specific branch from the repository, you have to run the git clone command with the -b flag and specify the branch and repository name.

There are two ways to clone a specific git branch. You can either clone the repository and checkout to a specific branch while fetching all branches. Or you can clone the repository while fetching only a single branch.

See the first method to clone a specific branch by fetching all the branches and checkout a specific branch.

First Method

Using these commands, it will fetch all the branches and checkout to one that you have specified. Then that specific branch will be your local branch and you can use the git push and git pull command to get and commit the task for that branch.

For example, you have a ‘dev‘ branch on your GitHub repository and want to clone it. So use the following command to do this.

As you see we use the -b flag with the git clone command. So what is mean exactly??

It means that it will fetch all the branches but you are only checking out the specific branch that you have specified in the command.

It means, when you execute the git branch with -a flag then you can see all the branches were fetched.

So this might not be what you exactly want, right? So let’s see the second method to fetch files from a specific branch, not all the files.

Second Method

To clone the specific branch by fetching only a single branch that you will specify. We will use –single-branch flag to clone a specific branch. This flag was introduced in Git 1.7.10 version. You can check your git version using this command git --version.

These commands will only clone the specific branch, not any other files from the other branches. You just have to specify the –single-branch flag with the git clone command.

The above command will only fetch the ‘dev‘ branch and ignore will other branches and files. You can check the branch with the git branch command and you will see only the ‘dev‘ branch was fetched.

Hope you like this tutorial and understand how to clone git branch and repository. If you have any doubt please do not hesitate to ask me. Put your queries in the comment section, I’ll respond to you as soon as possible.

You May Also Like

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *