Click here to Skip to main content
15,886,788 members
Articles / DevOps / Git

How to Check out a Remote GIT Branch, Which Doesn't Exist on Your Local Clone?

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Feb 2023CPOL2 min read 7.9K   2   1
Check out remote Git branch that does not exist on local clone
In this post, you will see the steps to check out a remote Git branch that doesn't exist on your local clone.

Imagine you are working on a git repository shared across multiple team members. Now you and one of your team members are working on a standard feature and must work collaboratively.

Your peer has created a new branch and made some changes and pushed. Now you have to check out the same branch in your local and continue making the changes on top of your peer's changes.

Below are the steps that need to be followed to check out the remote branch.

To check out a remote Git branch that doesn't exist on your local clone, you can use the following steps:

Step 1: Fetch the Remote Repository

Use the command git fetch origin to download the latest version of the remote repository, including any new branches.

In the below example, I have created a new branch in https://github.com/ulasalasreenath/TheBackendPro.

To fetch this new branch, I executed git fetch origin and the result is as follows:

From https://github.com/ulasalasreenath/TheBackendPro
     * [new branch]      dev        -> origin/dev

Step 2: Check the Available Branches

Use the command git branch -a to see a list of all the available branches, including both local and remote branches. The output of the command is as follows:

* main
remotes/origin/HEAD -> origin/main
remotes/origin/dev
remotes/origin/main

Step 3: Create a Local Tracking Branch

Use the command git checkout -b <branch-name> origin/<branch-name> to create a local tracking branch for the remote branch. The -b option tells Git to create a new branch, and origin/<branch-name> specifies the remote branch to track.

Command: 
  git checkout -b dev origin/dev
Output:
  branch 'dev' set up to track 'origin/dev'.
  Switched to a new branch 'dev'

Step 4: Push the Local Tracking Branch to Your Remote Repository

After making changes to the local tracking branch, you can push the changes back to the remote repository using the command git push -u origin <branch-name>. The -u option sets the local tracking branch to track the remote branch so that future git push and git pull commands are automatically directed to the correct branch.

Command:
git push -u origin dev
Output:
branch 'dev' set up to track 'origin/dev'.
Everything up-to-date

These steps will allow you to check out a remote Git branch that doesn't exist on your local clone and make changes to the branch. Once you have checked out the remote branch, you can work with it like any other local branch.

This article was originally posted at https://www.thebackendpro.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSimplify step 3? Pin
David On Life3-Feb-23 10:23
David On Life3-Feb-23 10:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.