Guidelines to use Git checkout remote branch:
- Commit to every task properly after completing it.
- Check all related changes are committed properly or not.
- Test the code properly before committing any task.
- The commit message should be clear for the user to get an idea of the task.
- 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 branchThe 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 originThe 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.