Git

How to checkout remote branch in Git

The branch is an essential part of any git repository. Multiple branches help manage the code properly. Any new feature can be tested without affecting the other code of the repository by keeping the files separately by using the branch. All commits are done by switching into the particular branch that contains new or updated files. The changes made in the local branch can be pushed into the remote branch, and the updated remote branch can be pulled into the local branch. `git checkout` command is mainly used to switch between the branches of the local repository and create a new branch locally. But this command can be used to check out the remote repository branch and how this command can be used for the branches of a remote repository, as shown in this tutorial.

Guidelines to use Git checkout remote branch:

  1. Commit to every task properly after completing it.
  2. Check all related changes are committed properly or not.
  3. Test the code properly before committing any task.
  4. The commit message should be clear for the user to get an idea of the task.
  5. Create necessary branches to manage the code.

Prerequisites:

Install GitHub Desktop.

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a GitHub account

You will require to create a GitHub account to check the commands used in this tutorial.

Create a local and remote repository

You have to create a local repository and publish the repository in the remote server to test the checkout command used in this tutorial for the remote branches.

Checkout Remote branch:

In this section, a demo remote repository named read-file is used for testing the commands to checkout remote branches. The local repository of this remote repository is created before. Here, the local repository contains a branch only, and a new branch has been created for the remote repository remotely. Open this remote repository from the GitHub Desktop. The following image shows that the remote repository has two branches named main and master.

Open the local repository from the terminal and run the following command to display existing branches of the local repository.

$ git branch

The following output shows that the local repository contains a branch named main.

Run the following command to fetch the changes made in the remote repository and check the updated content of the remote repository. The command will ask for the username and password of the GitHub account.

$ git fetch origin

The following output shows that the new branch named master is added to the remote repository.

Run the following commands to check the branch list of the local repository after running the fetch command and create a new local branch to track the remote branch.

$ git branch

$ get checkout -b master origin/master

$ git branch

The following output shows that after running the `git checkout` command, a new branch named master is created in the local repository to track the remote branch named master.

Go to github.com and log in to your account. Open the repository named read-file from the remote server. Create a new file named read3.php in the remote repository and commit the task. According to the following image, the remote repository contains three files. These are read.php, read2.php, and read3.php.

You can fetch the updated content of the remote repository from the terminal by running the git command or by opening the local repository from the GitHub Desktop and clicking the particular option. If you are not familiar with the git command and update the local repository with the graphical user interface, open the local repository from the GitHub Desktop. The following output shows that the local repository is not updated with the remote repository, and the last committed file in the repository is read2.php. To fetch the new content from the remote server, click on the Fetch origin button. If the local and remote repositories don’t match and any updated content is fetched from the remote repository, then the Pull origin option will be shown.

It has been shown in the previous step that a new file has been created in the remote repository. So, the following image shows the Pull origin option in the GitHub Desktop. Click on this option to retrieve the updated content from the remote repository and store the content in the local repository.

After clicking the Pull origin option, the local repository will contain the new file named read3.php that will be pulled from the remote server. The following output shows that the repository contains the new file and new commit message now.

In the previous task, the local repository has been updated with the content of the remote repository. But you can update the remote repository with the new content of the local repository also by using the terminal or GitHub Desktop. You have to run the `git push` command from the terminal to update the remote repository or click on the Publish origin option from the GitHub Desktop to update the remote repository with the new committed content of the local repository.

Conclusion:

This tutorial shows the use of the git checkout command to track any remote branch with the local branch. This feature of git is useful when new branches are created in the remote repository remotely, and the newly created branches don’t exist in the local repository.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.