To clone a single branch in Git, use the --branch parameter (or its shorthand -b) with the git clone command to specify the branch name. By default, git clone clones all branches of the remote repository, but this parameter allows you to clone only the specified branch. Additionally, using the --single-branch parameter instructs Git to fetch only the history of the specified branch.
Here is a practical command example to clone a single branch named feature-branch from the remote repository:
shgit clone --branch feature-branch --single-branch https://github.com/username/repo.git
This command performs the following:
--branch feature-branchor-b feature-branch: Specifies the branch to clone asfeature-branch.--single-branch: Instructs Git to clone only the specified branch, not all branches in the repository.https://github.com/username/repo.git: The URL of the remote repository.
Without --single-branch, Git still clones all branches, but the working copy will be checked out to the specified branch.
This approach is useful when you're only interested in a specific branch's content or aim to minimize data transfer during cloning, particularly for large repositories with numerous branches and history.