Clone Only Single, Specific Remote Branch in Git

To clone only single, specific a remote branch, use the git clone command followed by --single-branch flag, --branch flag and the remote repository url.

git clone --branch <remote-branch-name> --single-branch <remote-repo-url>

Clone All Remote Branches in Git

The git clone command is used to create a copy of a remote repository on your local machine. By default, this command only clones the main branch (usually master or main).

git clone <remote-repo-url>

When you run git clone without followed by flags, the following actions occur:

  1. A new folder called repo is made
  2. It is initialized as a Git repository
  3. A remote named origin is created, pointing to the URL you cloned from
  4. All of the repository's files and commits are downloaded there
  5. The default branch is checked out

After cloning, navigate to the directory of the cloned repository:

cd <repository-name>

To fetch all branches from the remote, you can use:

git fetch --all

This command fetches all objects from the remote repository but doesn't create local branches for them.

After fetching, you can show all branches, including remote ones by git branch -a command.

To create local branches from remote branches, you have to check out each branch:

git checkout -b <local-branch-name> <origin/remote-branch-name>

Manually checking out each branch can be tiresome if the repository has numerous branches. Here's a simple command to automate the process:

for branch in `git branch -r | grep -v HEAD`;do
    git checkout -b $branch $branch
done

This command loops through each remote branch and creates a corresponding local branch.