Git

How to Copy Remote Branch to Local Branch in Git

In this era, Git is the most commonly utilized decentralized version control system for project tracking when multiple people are working as a team. On Git, local and remote branches are used to work efficiently and keep a backup of projects. However, sometimes, it is required to save the remote branch work locally on the system.

This study will discuss the method of copying the remote branch to the local branch in Git.

How to Copy Remote Branch to Local Branch in Git?

While making a copy of a remote branch, copy its content to a local branch but do not make any tracking relation with them.

To copy the remote branch to the local branch in Git with the specified purpose, follow the below procedure.

Step 1: Open Git Bash
Open up the “Git Bash” terminal on your system using the “Startup” menu:

Step 2: Navigate to Git Directory
Move to the Git repository where you want to place the copy of the remote branch:

$ cd "C:\Users\nazma\Linux_1"

Step 3: Check Branches
Execute the “git branch” command to check the list of all remote and local branches that exist in the Git repository:

$ git branch -a

As you can see in the image below, our local repository contains “copy_branch”, “main”, and “master”, and two remote branches:

Step 4: Create Branch Copy
Create a copy of the remote branch in the local repository using the “git switch” command with the “-c” option. For instance, we want to create a copy of the “origin/copy_branch” remote branch, and the “new_copy_branch” is the name specified for the copied branch that is going to be placed in the local repository:

$ git switch -c new_copy_branch origin/copy_branch

Step 5: Check Branches List
After doing so, run the simple “git branch” command to check the list of local branches:

$ git branch

The given output indicates that we have successfully copied the remote branch as “new_copy_branch” in the current directory:

Or, run the “git branch” command with “-a” flag to enlist all remote and local branches:

$ git branch -a

We have provided the process of copying the remote branch to the local branch in Git.

Conclusion

To copy the remote branch to the local branch in Git, first, open the Git Bash, and navigate to the local directory. Then copy the remote branch locally using the “$ git switch -c” command and switch to it. After that, check the Git local branches list by executing the “$ git branch” command to verify the newly copied branch. In this study, we have discussed the method of copying the remote branch to the local branch in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.