Git checkout remote branch in one line

Eyecatch

Introduction

This article explains how to checkout a remote branch of Git and its detailed mechanism. It also introduces some useful commands for handling remote branches in addition to checkout.

How do I checkout a remote branch?

In conclusion, the only command to checkout a remote branch is the following. That's easy! In the following command, [branch_name] should be the name of the remote branch you want to checkout. (*Only modern Git version 1.6.6 or later is supported, so it may not work with older versions of Git)

git fetch && git checkout [branch_name]

How does it work?

Using this command, you can perform the following three operations simultaneously. If you have difficulty understanding the following, please refer to the following sections for further explanation.

  • Create a remote branch tracking branch (such as origin/branch_name).
  • Create a local branch with the same branch name as the remote branch.
  • Checkout the local branch that was created.

Note here that the branch name specified in this one-liner is not a tracking branch like origin/branch_name, but only branch_name. By selecting only branch_name, you can create locally.

What are remotes branches?

This section explains the concept of a "remote branch" in Git, which is necessary to understand why the previous command works.

No Central Server in Git

Now that we've covered the specifics, what is a git remote anyway? A Git remote is the same Git repository external to you; because Git is a distributed version control system, there is no "central" repository that is correct, and the same Git repository is maintained in multiple locations. Therefore, any repository that is outside of your local area on the Internet is remote. And the branch in the remote is called the "remote branch" in Git.

Remote branches cannot be written directly

It is important to note that the remote branch cannot be manipulated directly. So, instead of writing directly to remote, you need to copy the branch to local. So, what is the process of copying a branch to local?

Specifically, a "remote tracking branch" is created when you refer to a remote branch locally. This "remote tracking branch" is a referenced pointer that holds the state of the remote branch. In other words, it's like a bookmark that shows where the branch was pointing when you connected to the remote repository.

On the other hand, if this remote tracking branch is not local, there is no way to reference the remote branch from local, so you cannot checkout.

If the remote branch is updated when you do a fetch operation, it will be updated automatically.

Can't checkout without remote information.

Based on the explanation so far, let's explain the details of the meaning of the first command.

As explained so far, if the remote information is not local or in the remote tracking branch, then there is no local information, and you cannot check out. For example, even if you have a branch named branch_name in remote, you may get the error "branch does not exist" when you enter the following command.

$ git checkout -b branch_name origin/branch_name
fatal: 'origin/branch_name' is not a commit and a branch 'branch_name' cannot be created from it

You need to create a tracking branch, which can only be checked out by doing a checkout after creating a local tracking branch with git fetch, so that the local tracking branch can be referenced.

Here's an example of the specific message and how the command works

$ git fetch origin branch_name
From https://github.com/account/repository
 * [new branch]      branch_name -> origin/branch_name

$ git checkout branch_name
Branch 'branch_name' set up to track remote branch 'branch_name' from 'origin'. Switched to a new branch 'branch_name'

Tips: How do I update remote branches?

How do I turn my local branch into a remote branch?

To apply the contents of your local branch to your remote branch, run the following command.

git push <remote> <branch_name>

With the push command, you can specify the target remote and its branch name, and your current local branch will be reflected in the specified remote with the specified branch name.

How do I create a local branch from a remote branch?

To create a new local branch based on a remote branch, use the "-track" option in the branch command.

$ git branch --track new_branch origin/new_branch 

You can also do this by using the "checkout" command. If you want your local branch to have the same name as the remote branch, you only need to specify the name of the remote branch.

$ git checkout --track origin/new_branch 

Conclusion

In conclusion, do the following. That's all there is to it. Note, however, that you should use modern Git, 1.6.6 or later, and that this branch_name does not refer to a remote branch, such as origin/branch_name.

git fetch && git checkout [branch_name].

If you're using Github for team development, you'll probably want to check out remote branches created by other team members. Understanding how to checkout a remote Git branch and how it works will help you develop more smoothly.

Logo

Secure your IaC with just a few clicks!

You can keep your IaC security for free. No credit card required.

Share This Post