
#Git checkout remote branch how to
We will now see how to copy a remote into a new local branch with this method. We see below that the newly created branch does not track the old remote branch.īetween this old way and the newly introduced git switch command, the git branch command learned the -copy option to copy a branch. We again pass the -no-track option because we do not want to create any tracking relationship between the two branches. We next create a new copy of this branch with the git branch command. (Make sure you fetch the branches first.) git checkout We first check out into the remote branch we want to copy. Git checkout remote branch to local was thus the old method to make a local copy. One of them was switching to another branch. Git checkout was a command that did many things in old Git versions. the Old Method to Copy Remote Branch to Local Branch in Git - git checkout It is best to use the git switch remote branch to make a copy of it in our local repository. If we now list out all the branches on our local repository, the copied branch, new_local_copy, does not track the branch we copied, another_branch. This command has created a new copy with the name new_local_copy and switched (checked) into it. We also give the -no-track option because we only want a copy of the branch and not a clone (later in this post).

We pass a couple of options - the -c flag to create the new copy branch (it does not exist beforehand). We now create a copy of the origin/another_branch with the git switch command.

We will copy the branch named another_branch to our local repository.įirst, we will fetch the remote branches to our local repository with the git fetch command. On the remote repository, it looks like this: We set up a remote repo with a few branches and list them with the git branch command. This is now the best-recommended method to copy a remote to a local branch. With its -create option, we can use it to create a new branch if it does not exist before we switch to it. Newer versions of Git (Git v2.23, Q3 2019 and later) introduced a new command - git switch to check into a different branch. This led to a lot of confusion among developers. It checks into a different branch and restores changes from a commit. the Latest and Best Way to Copy Remote Branch to Local Branch in Git - git switch -cĮarlier, the command git checkout was overloaded for multiple purposes.

The use case here is to create a backup or explore the contents, but we do not want to merge the changes into the remote branch. We only copy the contents to a local branch but do not create any tracking relation with the remote. We will discuss two older ways to do this - git checkout remote branch to local, and git branch with the -c flag.

This tutorial will demonstrate how to use the new git switch -c command to copy a remote branch to a local branch. Copy Remote Branch to Local Branch in Git With the -copy Option in the git branch Command.the Old Method to Copy Remote Branch to Local Branch in Git - git checkout.the Latest and Best Way to Copy Remote Branch to Local Branch in Git - git switch -c.
