Git checkout remote branch

In this tutorial let us look out how to use git checkout remote branch and related concepts. The git checkout command lets you navigate between the branches created by git branch. The repository you maintain on Github/VSTS/Bitbucket/Gitlab is the remote repository which you always clone on your local machine for collaboration. By default the cloned version refers to the remote origin of a particular branch which you have checkout.

Developers use git to manage each feature in a different branches which is a dramatic shift from the SVN workflow. It makes the developers easy to experiment lot of things on a local branch and also no need to fear on destroying the remote branch code. Once the functionality is completed the developers can sync the feature branch to the remote branch.  

Managing Git Remotes

You could always use git remote and git remote show origin to find out the certain information about your git remote repository. As you can see it returns the output such as 

  • Remote repository URL
  • Remote Branch name
  • Remote tracking branch status

List all remote branches in Git

If you want to know the list of all the branches available in Git remote before doing a checkout then you can run a command git branch -a to list all the remote branches 

Git checkout remote branch

Git checkout let us to switch from the current branch into another branch. Below are few reason on switching branches in Git –

  • to switch from a one local branch to another local branch.
  • to create a new local branch from a git remote branch

Checking out the remote branch updates the files in the working directory to the latest code stored in the remote branch.

First fetch the remote branches using command git fetch 

Second checkout the remote branch by using git checkout command.
Replace branchname with your desired remote branch name.

Additionally you can checkout a new local branch and reset it to the remote branches last commit. Below command will checkout the branch master and reset it to remote branch last commit.

If you are using the older git version(< 1.6.6 ) then you need to slightly change the command for fetch and checkout the remote branches.

How do you track a remote branch during git checkout?

If multiple people want to work on the same branch in that scenario you need to use –track option with the git checkout command.

The below command ensure to create a new local branch with the same name as the remote one – and directly establishes a tracking connection between the two. 

Set up to track remote branch

Git Checkout Remote Branch Best Practices

Use git pull before pushing code – Ensure to pull the latest code from the origin as the other team mates might have pushed their changes into remote repository. Merging these changes into your local branch is necessary before you push anything to remote.

Have smaller Commit often – Ensure you split your task into smaller features and keep the commits small and do commit frequently so that you can avoid merge conflicts.

Commit only working code – Once you have finished with developing the feature and unit tested commit the code to remote. Do not merge or commit the non-working code. This might break the entire solution on remote.

Use branches – Create as many branches as you need . Follow a proper naming conventions for the branch you create.

Clear messages on Commit – Include a meaningful message while commit, this will ensure what functionality is been added or modified to other users.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.