Javaexercise.com

How To Check Out A Remote Branch In Git?

A remote branch is a branch that exists on a remote repository. Remote repositories are Git repositories hosted on platforms like GitHub and BitBucket.

A remote repo helps developers collaborate on a project and share their work with the rest of the world.

To checkout a branch means to move or switch to that branch. The Git Checkout command helps us to checkout or switch branches.

The Git Checkout command has several other functionalities. We can also use this command to create new branches.

Let's learn how to check out a remote Git branch.

Checkout a Remote Git Branch

To check out a remote branch, we first need to fetch or download the latest changes from the remote repo. We use the Git Fetch command to download the latest changes and update our local remote-tracking branches.

A remote-tracking branch is a local branch that tracks the changes of the corresponding remote branch. Remote-tracking branches have the name of the format remote-name/branch-name. For example, origin/feature is a remote-tracking branch that tracks the feature branch of the origin remote.

git fetch <remote-name>

We cannot directly switch to the remote-tracking branch because it would lead to a Detached-HEAD state. Any changes or commits that we make in a Detached-HEAD state will be lost.

So, we need to create a new local branch based on the updated remote-tracking branch. Here, we can use the Git Branch command. We will pass the new branch name and the name of the remote-tracking branch. The following command will make the new local branch track the changes of the remote branch, just like the remote-tracking branch.

git branch <new-local-branch-name> <remote-tracking-branch-name>

Finally, we can check out the newly created branch. This branch contains all the commits of the corresponding remote branch. We can also add new commits to this branch.

git checkout <new-local-branch-name>

Example

Consider a remote repository with two branches - master and feature.

Remote Repository
​
A -- B -- C -- D <- master
          \
           \
            E <- feature

Also, consider our local clone of the remote repo. Our local repository is not up-to-date and does not contain the commit E on the origin/feature branch. The origin/feature branch is a remote-tracking branch that tracks the changes of the feature branch of the remote repo.

Local Repository
​
A -- B -- C -- D <- master <- HEAD, origin/master
         ^
    origin/feature

We can verify the branches by using the Git Branch command with the -a flag.

git branch -a
Output
​
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature
  remotes/origin/master

Our aim is to checkout the feature branch of the remote repository.

First, let's update the remote-tracking branches. To update the remote-tracking branch, origin/feature, with the new commit(commit E), use the Git Fetch command. The new commit E will be added on the origin/feature branch.

git fetch origin
Local Repository

A -- B -- C -- D <- master <- HEAD, origin/master
		  \
		   \
		  	E	<- origin/feature

We can directly checkout the origin/feature branch. But our repository will go in a Detached-HEAD state. Let's try this and view the output.

git checkout origin/feature
Output

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 82b49ed Message for Commit E

If we don't want a detached HEAD, then we need to create a new branch. Let's create a new branch that points to the origin/feature branch. Let's call this branch local-feature.

git branch local-feature origin/feature
Output

Branch 'local-feature' set up to track remote branch 'feature' from 'origin'.

Finally, we can switch to that branch by using the Git Checkout command.

git checkout local-feature
Output

Switched to branch 'local-feature'
Your branch is up to date with 'origin/feature'.
Local Repository

A -- B -- C -- D <- master <- HEAD, origin/master
		  \
		   \
		  	E	<- local-feature <- HEAD, origin/feature

Conclusion

Remote branches help us collaborate with other developers and view the changes made by them. Checking out a remote branch can be done in a few simple steps. These steps are summarized below.

  1. First, we need to download all the latest changes made to the remote branches. To do so, use the Git Fetch command and pass the remote name(origin, in most cases).

    git fetch origin

     

  2. Now, we will have an updated remote-tracking branch that tracks a remote branch. We can directly checkout that remote-tracking branch, but this will lead to a Detached-HEAD state.

    git checkout origin/feature 	# leads to Detached-HEAD

     

  3. Next, we will create a new branch that points to this remote-tracking branch. This new branch will also track the remote branch. It makes pushing, pulling, and fetching easier.

    git branch local-feature origin/feature

     

  4. The final step is to checkout or switch to the new branch. We can use the Git Checkout command to move to the local branch.

    git checkout local-feature

     

  5. We can perform steps 3 and 4 with a single command. We can either use Git Checkout with -b or we can use Git Switch with -c. These commands will create a new branch and will also switch to it.

    git checkout -b local-feature origin/feature
    git switch -c local-feature origin/feature

     

By following the above steps, we can check out a remote branch.