How to Check out a Remote Git Branch [Step

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

Git is a free and open source distributed version control system that allows anyone to view the history of any file or collection of files. It can also be used to share changes between multiple people, which makes it ideal for collaborative coding projects where there are several developers working on different parts of an application. This guide will outline how to check out from a remote branch in your Git repo.

“git checkout a remote branch” is a command-line tool that allows users to check out a remote branch.

How to Check out a Remote Git Branch [Step

Have you been asked to checkout a remote branch of a Git code repository when you first joined a team? Remote Git branches, which are used by both big and small teams, provide extensive code collaboration capabilities. Learn and understand how to obtain code from a remote Git repository in this post!

Prerequisites

Chocolatey and Git4Win are two more possible Windows installation techniques.

  • GitLab, GitHub, or Azure DevOps are examples of remote Git repositories. You may learn from the repositories mentioned in this article, or you can create your own.

What is the definition of a remote branch?

A branch in a Git repository stores and tracks a sequence of connected commits. A commit is saved by default in the presently assigned branch, which is usually main or master.

A remote branch is a Git repository branch that lives on another server. Once enabled, your local Git repository will seek for external commit changes in these remote repositories, referred to as “remotes.”

With the Git branch command, you can see which branches are accessible. The presently active branch is indicated with a star (*). Because no distant branches exist yet, the branches depicted below are simply local branches.

The git branch command returns a list of local branches.The git branch command returns a list of local branches.

What is a Remote Branch in Git Checkout?

Let’s say you have a local Git repository and want to collaborate with others. Use the Git checkout command to get the most recent commits from a remote branch on a remote Git repository.

With a configured remote branch, Git will instead mirror a remote branch instead of a local-only branch. An example of the command git checkout remote <branch> is shown below.

Clone the Git Repository Offline

You may get started right away since you already have a repository. To clone the remote repository to a local folder, follow the steps below.

1. Cloning a remote repository over HTTPS rather than SSH may be the simplest way to circumvent certificate and firewall difficulties.

  • Go to GitHub and find the repository.
  • Select HTTPS from the Code drop-down menu.
  • To copy the URL to the clipboard, click the Clipboard icon next to the link.

In GitHub, find the HTTPS clone URL.In GitHub, find the HTTPS clone URL.

2. Open a command prompt using your preferred CLI, such as Windows Command Prompt or Windows Terminal, after copying the HTTPS URL to your clipboard.

3. Select a folder in which you want the cloned repository to be saved. As seen in the pictures, this tutorial makes use of the git folder in the home directory. To clone the repository, use the following command, which will create a subfolder containing the remote repository.

# GitHub git clone <https://github.com/Adam-the-Automator/git-checkout-remote-branch>

Cloning a remote repository to your local computer.Cloning a remote repository to your local computer.

4. Use the command cd git-checkout-remote-branch to get to the cloned repository.

Verify that the repository has been successfully established using the status command. The Git status tool shows the differences between the local and remote branches, which might help you figure out whether your branch is connected and up to date! The default Git remote, as prefixed to the primary remote branch, is called origin. status of git

Checking the status of a repository. Checking the status of a repository.

The default primary branch in most current Git applications is main. An older program could use master as its default branch name, although both names are functionally equivalent. Main, RemoteBranch, SomeBranch, and AnotherBranch are the branches used in this lesson.

How to Track Branches Using a Git Remote

When you clone a repository, it immediately establishes a remote source reference. Check for new commits or submit new changes to the remote repository using this reference.

Although a Git repository may contain several remotes, a branch can only reference one.

A relationship is formed when a distant branch is tracked. This connection makes it simple to push or pull commits from the remote branch to the local branch. A tracked branch also specifies how much ahead or behind the remote branch in commits the local branch is.

Remotes for Git Repositories Listing

To begin, examine whether the repository you’re working with contains any Git remotes. To do so, use the git remote command to see Git remotes in your repository, as illustrated below. The origin and remote2 remotes are presented in this example.

Viewing a repository from two different locations.Viewing a repository from two different locations.

Origin and remote2 are two more monitored remote repositories, where your remotes may vary.

Add the v option to show the complete Git remote URI to view additional information about the specified remotes.

Viewing a lot of information about repo remotes.Viewing a lot of information about repo remotes.

You may be wondering why fetch and push have different URIs. The triangle workflow is a Git process that allows you to check for updates to a repository at a separate URI than where you may contribute changes.

Viewing the Remote Branch Options

The branch command displays all accessible branches in your repository, both local and remote. Append the all switch to display the remote branches as well, as illustrated below.

In the picture below, you can see that the tutorial’s remote repository origin is shown correctly. This snapshot, however, is missing all branches from the Remote2 remote.

Viewing all branches in a repository (including remotes).Viewing all branches in a repository (including remotes).

If you know a branch is on your remote but it doesn’t appear in the branch list, you’ll need to update your list of accessible remote branches, as explained in the following section.

Updating the Remote Branches That Are Available

A remote repository may sometimes add or update branches. You may need to use the git fetch command to ensure you’re working with the latest recent branches.

The fetch command connects to the remote repository and returns a list of all branches that are accessible. After performing the git fetch command, check your currently tracked branches to see whether the remote branch is now visible, as seen below.

# git retrieve all available remote branches # Use git branch —all to check that all branches are now accessible on the local Git repository.

The branch remotes/origin/RemoteBranch is now visible in the list of accessible remote branches, as seen below.

To update the list of accessible remote branches, use git fetch.To update the list of accessible remote branches, use git fetch.

Fetch only looks for remote changes that are presently being monitored in the current branch. Add the all argument to have Git check for changes from all remotes in the repo, regardless of tracking status.

Add the remote name to the git fetch command to get the available branches from a particular remote, such as Remote2 in the example below, which returns the branch main.

# git branch —all a local Git repository’s available branches # git fetch get available branches from remote2 Remote2 # Use git branch —all to check that the remote2 branch, main, is now accessible.

Using the git fetch command, fetching main from remote2.Using the git fetch command, fetching main from remote2.

Git compares commits with the remote branch whenever you use git get, git pull, or git push to check for remote changes. Any Git remote branch may be tracked by local branches. Multiple local branches may also be configured to monitor the same distant branch.

Viewing the Remote Tracking of Each Branch

To see the tracked remote branch and corresponding local branch, use the vv parameter with the command git branch. The command results are a list of remotes and branches in the format [<remote>/<branch>] as shown below.

All local branches' remote tracking is listed.All local branches’ remote tracking is listed.

Changing the Remote Tracking of a Branch

To monitor a different distant branch, you may need to alter your local branch. Track the remote branch using the u option to set your current branch to a certain remote branch, as seen below.

Remote2/main git branch -u

Setting the current branch's remote to track.Setting the current branch’s remote to track.

Examining Git Remote Branches

You may need to checkout a different branch with freshly formed branches on the remote Git repository. The checkout command is used to check out a remote repository branch.

Git will automatically find the correct remote, download a local copy of the remote branch, and configure the local copy to track against the remote branch if the following requirements are fulfilled. Examine the output of the git branch —all command to ensure the following requirements are not satisfied.

  • There isn’t another local branch with the same name.
  • There are no multiple remotes with the same remote branch name.

Use the git checkout command and enter the name of the remote branch as shown below to execute the checkout.

RemoteBranch git checkout

Using git checkout to create a local branch from a remote branch.Using git checkout to create a local branch from a remote branch.

If a local branch with the same name already exists in your local repository, use the b argument to provide a different local branch name before checking out the remote branch.

git checkout -b origin/RemoteBranch AnotherBranch

Creating a local branch with a different name than the remote branch.Creating a local branch with a different name than the remote branch.

Specify the remote and branch to monitor local commits against if your local repository has several remotes with the same branch name.

—track origin/RemoteBranch git checkout

Creating a tracking-enabled local clone of a remote branch.Creating a tracking-enabled local clone of a remote branch.

For branch switching and creation, future Git versions may prefer that you use the git switch command. The Git switch function is limited to branches, but the Git checkout command offers a wide range of capabilities. If you want to learn more about the logic, Irshad S. created a good primer on it.

Pushing a Local Branch to a Remote

You may use the git push command to push a local branch to a remote branch that doesn’t have any remote branches for tracking. The u argument is the easiest way to start a new branch on your preferred remote.

The u option, which you used above, needs the repository name and source branch inputs. Git knows to use the current local branch name as the name on the remote by merely supplying HEAD as the source branch name, in this instance NewBranch.

# Check out and change branches to NewBranch # Create NewBranch on the origin remote using git checkout -b NewBranch. HEAD git push -u origin # Check that the distant branches are still there. branch -vv git

A local branch is being pushed to an upstream remote called origin with the same name as local.A local branch is being pushed to an upstream remote called origin with the same name as local.

The current branch is simply referred to as HEAD. Other branches may be pushed to the remote and tracked by using their names instead of HEAD.

There are situations when the name of the current local branch does not match the name of the distant branch. If you want to use a different name, provide it after the source is joined by a colon, as seen below.

# git push -u origin HEAD:AnotherBranch creates the current branch on the origin remote as AnotherBranch # Check that the local NewBranch has been renamed to AnotherBranch on the remote git branch -vv

Using an alternative remote branch name to push a local branch to an upstream remote called origin.Using an alternative remote branch name to push a local branch to an upstream remote called origin.

Additional Resources and Next Steps

You’ve now gone through the fundamentals of Git, including how to handle remotes and monitor remote branches. You’ve also learned how to check out a remote branch in numerous methods and several Git recommended practices. This understanding will assist you in avoiding future frustration.

If you need more information on other elements of administering a Git repository, check out the following useful Git resources:

  • When you inadvertently have your HEAD detached and when you’d genuinely prefer it that way, here’s a primer on detachable heads.
  • Unlike Git checkout, Git switch is just for dealing with branches.

To checkout a remote branch, you can use the “git clone” command. This will create a local copy of the remote repository on your computer. Reference: git clone a remote branch locally.

Related Tags

  • git checkout branch
  • git create new branch from remote
  • git checkout remote branch first time
  • git switch to remote branch
  • git list remote branches