How to checkout remote branch in git


To checkout a remote branch in Git, you first need to fetch the latest changes from the remote repository using the git fetch command. This will update your local repository with the latest changes from the remote repository, including any new branches that have been created.

Once you have fetched the latest changes, you can checkout the remote branch using the git checkout command with the -b option to create a new local branch that tracks the remote branch:

$ git checkout -b <local-branch-name> <remote-name>/<remote-branch-name>

For example, to checkout the development branch from the origin remote repository and create a new local branch named dev that tracks the remote branch, you can use:

$ git fetch origin
$ git checkout -b dev origin/development

This will create a new local branch named dev that tracks the development branch in the origin remote repository, and checkout the branch so that you can start working on it.

If you already have a local branch with the same name as the remote branch you want to checkout, you can omit the -b option and use the git checkout command with the remote branch name to switch to the branch and track the remote branch:

$ git fetch <remote-name>
$ git checkout <remote-name>/<remote-branch-name>

For example, to switch to the development branch and track the remote branch in the origin remote repository, you can use:

$ git fetch origin
$ git checkout origin/development

This will switch to the development branch and track the remote branch in the origin remote repository.

Note that when checking out a remote branch, you should always make sure that you have committed or stashed any changes in your local repository to avoid conflicts.