How to clone a specific branch without switching branches on the remote repository?

How to clone a specific branch without switching branches on the remote repository?

You can clone a specific branch from a remote repository without switching branches on the remote repository by using the --branch or --single-branch option with the git clone command.

Here's how you can clone a specific branch using the --branch option:
git clone -b <branch-name> <remote-repo-url>

For example, to clone the develop branch from the remote repository git@github.com:user/repo.git, you can use the following command:
git clone -b develop git@github.com:user/repo.git

Alternatively, you can use the --single-branch option to clone only a single branch, which can save time and bandwidth when cloning large repositories. Here's how you can use the --single-branch option:
git clone --single-branch -b <branch-name> <remote-repo-url>

For example, to clone only the develop branch from the remote repository git@github.com:user/repo.git, you can use the following command:
git clone --single-branch -b develop git@github.com:user/repo.git

These commands will clone only the specified branch from the remote repository to your local machine without switching branches on the remote repository.



Back to The Programmer Blog