W3docs

How to Clone Remote Git Branches

Cloning is an essential process in Git. In this tutorial, you will find a solution to the problem of cloning remote branches and copy the codes right away.

The idea of cloning is to download the complete source code to the local machine. Since it is a very important process in Git, we will show how you can clone branches from the remote repository.

Steps to clone remote branches

Here we suggest 4 steps that you can take to get a copy of a remote git branch:

Cloning the remote and changing directory

Firstly, clone the remote repository and cd into it.

Clone and change directory

git clone git://example.com/exampleProject
cd exampleProject

Listing branches

Secondly, use the git branch command for listing the branches and git branch with the <kbd class="highlighted">-a</kbd> option for listing all remote branches:

List local branches

git branch // Lists local branches

List local and remote branches

git branch -a // Lists local and all remote branches

Checking out the remote locally

Then, check out the remote branch locally. To create a local branch that tracks the remote one, use:

Check out the remote branch

git checkout -b <branch> origin/<branch>

Fetching the remote

Now let’s fetch the remote updates:

Fetch remote updates

git fetch origin

Concept of Cloning

When cloning a repository, all the files are downloaded to the local system without affecting the remote git repository. The git clone command clones and copies an existing repository into a new directory. It is also used to create remote-tracking branches for each branch in the cloned repository. It also allows users to get a development copy of an existing central repository.

Remote Branches

The git branch command creates, lists, and deletes branches. It operates on both local and remote branches. Remote branches are actually local remote-tracking branches that point to the state of branches in the remote repository. They are updated locally when you run git fetch or git pull, serving as a reference to where the remote branches were the last time you connected.

Note: Use git pull to sync your local branches with remote updates after cloning or fetching.