How to Copy Remote Branch to Local Branch in Git

Ankit Raj Goyal Feb 02, 2024
  1. the Latest and Best Way to Copy Remote Branch to Local Branch in Git - git switch -c
  2. the Old Method to Copy Remote Branch to Local Branch in Git - git checkout
  3. Copy Remote Branch to Local Branch in Git With the --copy Option in the git branch Command
  4. Difference Between Git Copy and Clone
How to Copy Remote Branch to Local Branch in Git

This tutorial will demonstrate how to use the new git switch -c command to copy a remote branch to a local branch. We will discuss two older ways to do this - git checkout remote branch to local, and git branch with the -c flag.

We only copy the contents to a local branch but do not create any tracking relation with the remote. 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.

the Latest and Best Way to Copy Remote Branch to Local Branch in Git - git switch -c

Earlier, the command git checkout was overloaded for multiple purposes. It checks into a different branch and restores changes from a commit.

This led to a lot of confusion among developers.

Newer versions of Git (Git v2.23, Q3 2019 and later) introduced a new command - git switch to check into a different branch. With its --create option, we can use it to create a new branch if it does not exist before we switch to it.

This is now the best-recommended method to copy a remote to a local branch.

We set up a remote repo with a few branches and list them with the git branch command.

git branch -a

Setup Repo Multiple Branches

On the remote repository, it looks like this:

Branch To Copy On Remote

We will copy the branch named another_branch to our local repository.

First, we will fetch the remote branches to our local repository with the git fetch command.

git fetch --all

We see this fetches the remote branches.

Fetch Remote Branches

We now create a copy of the origin/another_branch with the git switch command.

We pass a couple of options - the -c flag to create the new copy branch (it does not exist beforehand). We also give the --no-track option because we only want a copy of the branch and not a clone (later in this post).

git switch -c <new_branch> <old_branch> --no-track

Git Switch New Local Copy

This command has created a new copy with the name new_local_copy and switched (checked) into it.

Switch No Tracking Relation

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.

It is best to use the git switch remote branch to make a copy of it in our local repository.

the Old Method to Copy Remote Branch to Local Branch in Git - git checkout

git checkout was a command that did many things in old Git versions. One of them was switching to another branch.

Git checkout remote branch to local was thus the old method to make a local copy.

We first check out into the remote branch we want to copy. (Make sure you fetch the branches first.)

git checkout <remote_branch>

We next create a new copy of this branch with the git branch command. We again pass the --no-track option because we do not want to create any tracking relationship between the two branches.

git branch <new_branc> --no-track

Git Copy With Checkout

We see below that the newly created branch does not track the old remote branch.

Copy Checkout No Tracking Relation

Between this old way and the newly introduced git switch command, the git branch command learned the --copy option to copy a branch. We will now see how to copy a remote into a new local branch with this method.

Copy Remote Branch to Local Branch in Git With the --copy Option in the git branch Command

It is a two-step procedure.

We first check out the remote branch with the git checkout command. Note the --no-track option.

git checkout <old_branch> --no-track

We next copy into a new branch by passing the -c flag (for --copy) to the git branch command.

git branch -c <old_branch> <new_branch>

Git Copy With Branch Command

Again, you can see below that the new copy does not track the old branch.

Git Copy With Branch No Tracking Relation

Difference Between Git Copy and Clone

Git copy is not the same as Git clone. There are a few subtle differences.

  1. When you copy a branch, the new branch does not track the old branch. It means that there is no connection between the two - the new branch merely has copied the contents of the old branch.

    However, a clone tracks the old branch. This means that you can push, pull, merge, rebase and use these commands in their vanilla form without passing explicit arguments - the tracking relation fills in the branch names automatically.

  2. In Git, cloning is optimized for efficiency. So, when you clone, the new branch does not get the old branch’s reflogs, configs, and hooks.

    A copy does copy all these as well. It is vital in some instances, e.g., when you try to recover lost commits with the help of reflog.

  3. A Git copy operation has a few typical use cases - most commonly, to create a backup of a remote branch. Another use case is when a developer wants to download and explore an interesting remote feature branch without any intention of pushing his changes to the main codebase.