DEV Community

Mrityunjaya Prajapati
Mrityunjaya Prajapati

Posted on

How to Clone a Specific Git Branch

What is Git

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.

How to clone a Git Repository

Git Clone https://github.com/deadwin19/nest-rest-mongo-boilerplate.git

This gives you access to all branches in this repository and you can easily toggle between different branches and see the files.

You can use following command to change your local branch

git branch -a

How to Clone a Specific Branch

Option One

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

OR

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

With Option One, you will fetch all the branches in the repository but checkout to the one that you specified, and that branch becomes the configured local branch for git push and git pull.

Option Two

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

OR

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

Above step performs the same action as option one, except that it will only fetch files from the specified branch without fetching other branches.

Conclusion

Git Clone for Single Branch saves good amount of disk space specially when you have to work on only specific branch.

If you liked the solution, please hit the Clap button or comment if you have any suggestion or feedback.

Top comments (0)