乐闻世界logo
搜索文章和话题

How do I clone a single branch in Git?

1个答案

1

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:

sh
git clone --branch feature-branch --single-branch https://github.com/username/repo.git

This command performs the following:

  • --branch feature-branch or -b feature-branch: Specifies the branch to clone as feature-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.

2024年6月29日 12:07 回复

你的答案