To clone a single branch from a remote Git repository, use the git clone command with the --single-branch option. This allows you to clone only the specified branch, rather than all branches in the repository. This method saves time and storage space, especially when the remote repository contains numerous branches and data.
The specific steps are as follows:
-
Open your terminal.
-
Use the
git clonecommand with the--single-branchoption, specifying the branch name and the remote repository URL. The format is:bashgit clone --single-branch --branch <branch_name> <repository_url>Where
<branch_name>is the name of the branch you want to clone, and<repository_url>is the URL of the remote repository.
For example, if you want to clone the dev branch from a GitHub repository with the URL https://github.com/example/repo.git, the command would be:
bashgit clone --single-branch --branch dev https://github.com/example/repo.git
This command creates a local directory named repo containing only the content of the dev branch.
This method is ideal for quickly obtaining a specific branch for development or testing purposes.