How to Pull a Remote Branch in Git
One of the most popular version control programs is Git. It is open-source and free, and it enables you to carry out a variety of complicated tasks to organize your workflow to create a project that is easy to collaborate on.
In this guide, we will look at three methods with which you can directly fetch or pull a remote branch in git. With these commands, you will be able to checkout to a remote branch directly from your local branch!
Related: How to overwrite local branches with remote branches in git

Method 1: Using the new git switch command
Git has recently introduced a new command called switch
which makes it super easy to pull a remote branch.
For example, if my_remote_branch
is a branch that exists on your remote repository and you want to pull it locally, type the following command into the terminal:
$ git switch my_remote_branch
If you don't have a local branch called my_remote_branch
, this will make git automatically scan the remote repository to see if a branch of that name exists. If it does, it will pull that remote branch and checkout to it.
Related: Git rename branch

Method 2: Using git fetch
Before the introduction of git switch
, you would have to follow the old method to pull a remote branch - by using git fetch and then checking out into the fetched branch.
First, fetch the remote branch from your repository locally using the command:
$ git fetch origin <yourRemoteBranch>
Then, checkout into the fetched remote branch using the command:
$ git checkout <yourRemoteBranch>
Method 3: Create a local branch that tracks the remote branch
The final method to fetch or pull remote branches is to set up tracking between local branches and remote branches. The command to do this is:
$ git checkout --track remotename/your_remote_branch
Here, remotename
is the name of your remote repository and your_remote_branch
is the name of the remote branch that you want to track!
And that's all folks. Here's how you can pull a remote directly from your remote git repository in one (or three) single commands!