How To Clone a Specific branch in git

Git is by far the most widely used version control system for programmers.

You will almost certainly have distinct features when working on a project. This project and its features will be worked on by a number of contributors.

Branches allow you to make a “playground” out of the master branch’s files. You can use this branch to create separate features, test new features, make breaking changes, generate fixes, write documentation, and experiment with new ideas without breaking or affecting the production code. When you’re finished, merge the branch into the main branch for production.

Git is a distributed environment created in 2005 by Linus Torvalds, the founder of the Linux operating system. It allows numerous engineers and teams to collaborate on the same codebase. You can either create your own Git repository or clone an existing Git repository to begin working with Git.

We’ll concentrate on cloning an existing Git repository in this lesson.

We’ll also look at how to clone a specific branch, how to clone with an SSH key, and how to resolve access forbidden difficulties.

Clone a Git repository

To clone a git repository, use “git clone” followed by the URL of the repository you want to clone.

git clone <url>

Let’s say you wish to clone a public repository from Github. You’ll run the command below.

git clone https://github.com/username/project.git

Cloning into 'project'...
remote: Enumerating objects: 813, done.
remote: Total 813 (delta 0), reused 0 (delta 0), pack-reused 813
Receiving objects: 100% (813/813), 3.66 MiB | 5.52 MiB/s, done.
Resolving deltas: 100% (391/391), done.

As you can see, Git will clone your Git repository into a folder named after the project name by default.

You can clone your Git repository into a separate folder, though.

Clone a specific branch

Execute the “git clone” command and specify the destination folder at the end to clone a git repository into a specific folder.

git clone <url> <directory>

For example, if we wish to clone the Github project we got in the previous part into a folder called “myproject,” we would run

git clone https://github.com/username/project.git myproject

Cloning into 'myproject'...
remote: Enumerating objects: 813, done.
remote: Total 813 (delta 0), reused 0 (delta 0), pack-reused 813
Receiving objects: 100% (813/813), 3.66 MiB | 5.65 MiB/s, done.
Resolving deltas: 100% (391/391), done.

Check to see if your git project was successfully cloned to the destination location.

ls -l
total 4
drwxrwxr-x 5 schkn schkn 4096 Nov  1 10:39 myproject

Awesome!

You’ve successfully cloned a Git repository to a given server folder.

You cloned the master branch from your Git remote repository in this situation.

The “git branch” command can be used to check the current cloned branch.

git branch
* master

In other circumstances, though, you may need to clone a specific branch in order to get started.

For example, your team may have decided to leave the “master” branch a little behind and push the most current commits to the “dev” branch.

Reference

Leave a Comment

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