How do I clone a specific Git branch?

How do I clone a specific Git branch?

When you clone a Git repository, you create a local copy of the repository on your machine. This local copy includes the entire repository, including all branches and commits. By default, when you clone a repository, the git clone command will automatically create a local copy of the master branch.

However, you can use the --branch option to specify a different branch to clone. This is useful if you want to work on a specific branch and don't need the other branches in the repository. For example, you might use this if you want to clone a development branch and work on a new feature.

Here's an example of how you might use the git clone command to clone a specific branch:

git clone --branch my-branch https://github.com/user/my-repo.git

This will create a local copy of the my-branch branch in a new directory with the same name as the repository. The directory will be created in the current directory where you ran the git clone command.

You can also specify the name of the directory where you want to clone the repository using the -o option. For example:

git clone --branch my-branch -o my-repo https://github.com/user/my-repo.git

This will create a directory called my-repo in the current directory and clone the my-branch branch into it.

Once you've cloned a specific branch, you can switch between branches using the git checkout command. For example, to switch to the master branch, you would run the following command:

git checkout master