

Please note that the git switch was added in Git 2.23, prior to this git checkout was used to switch branches. As already said, creating a remote branch actually starts on the opposite end: in your local Git repository You need to make sure you have a local branch that represents a state you want to push to the remote. (or git branch -a to see also the list of remote branches). The image below shows how the fetch is different to pull and the way it operates:. Pull changes from a remote branch Note that you cannot make changes directly on a remote branch. For the remote branches, you'll find them prefixed with remotes/origin. With the remote branches in hand, you now need to check out the branch you are interested in with -c to create a new local branch:įor more information about using git switch : git branch -a The output of this command is the list of branches available for checkout. Which is going to help you get all of the remote branches for you and with the help of the line below, can see the branches available for checkout: However, in the case where multiple remote repositories exist, the remote repository needs to be explicitly named. This can be can be disabled with -no-guess In this case Git is guessing that you are trying to checkout and track the remote branch with the same name. The branches that start with remotes/* can be thought of as reading only copies of the remote branches and to work on a branch you need to create a local branch from it which is done with the Git command switch (since Git 2.23) by giving it the name of the remote branch excluding the name of the remote as mentioned below: This will help you in fetching all of the remote branches for you and you will be able to see the branches available for checkout with:

With a single or multiple remotes, start by fetching from the remote repository to make sure you have all the latest changes updated and downloaded. Updated for Git 2.23: For older versions check the end section This would be because the single remote case along with some of the commands can be simplified as there is less ambiguity. 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.The answer to your question has been divided depending on whether there is one remote repository configured or various of them which are configured. This will switch to the development branch and track the remote branch in the origin remote repository. 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 įor example, to switch to the development branch and track the remote branch in the origin remote repository, you can use: $ git fetch origin 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. 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 /įor 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 a remote branch update#
This will update your local repository with the latest changes from the remote repository, including any new branches that have been created. To checkout a remote branch in Git, you first need to fetch the latest changes from the remote repository using the git fetch command.
