Git

How to Replace Local Branch With Remote Branch Entirely in Git

Git branches are the basic unit of Git repositories where developers can manage and test source code and implement new features and functions on it. Git users work correspondingly with branches of the local repository and remote repository. Due to that, occasionally, the local commits and changes are messed up in the local branch.

In this article, we will teach:

How to Replace the Local Branch With Remote Branch by Rebuilding the Local Branch?

To replace the local branch with a remote branch completely, first, we will delete the local branch. Then, fetch the remote branch through which you want to reset the local branch. After that, rebuild the local branch and reset it to the remote version using the “git checkout -b <branch-name> origin/<remote-branch-name>” command.

To completely replace the local branch with a remote branch, utilize the below-mentioned procedure.

Step 1: Open Git Terminal
Firstly, launch the Git Bash terminal from the Start menu:

Step 2: Open Git Repository
Open the Git repository through the “cd” command:

$ cd "C:\Git"

Step 3: View Git Local Branches
View all local branches using the “git branch” command and choose the branch you want to replace with a remote branch. For instance, we have selected the “master” branch:

$ git branch

Step 4: Delete Branch
Delete the selected branch using the “git branch” command with the “-D” option and specify the branch name in it:

$ git branch -D master

Step 5: Fetch Remote Branch
Next, fetch the remote branch that will be used for resetting the local branch using the “git fetch origin” command:

$ git fetch origin master

Step 6: Replace Local Branch With Remote
Next, rebuild the branch and set it to the remote version using the “git checkout” command. As a result, the new branch is directly reset like the remote master branch:

$ git checkout -b master origin/master

How to Replace the Local Branch With Remote Branch by “git reset” Command?

To replace the local branch with the remote branch using the “git reset” command, check out the below-mentioned steps.

Step 1: Move to Branch Needed to Replace
First, go to the branch you need to replace using the mentioned command:

$ git checkout master

Step 2: Fetch Remote Branches
Next, fetch all remote branches using the “git fetch <remote-name>” command:

$ git fetch origin

Step 3: Replace Local Branch With Remote
After that, replace the local branch completely with a remote branch through the “git reset” command:

$ git reset --hard origin/master

To verify if the branch is reset same as a remote branch or not, checkout the Git logs:

$ git log

The output indicates that we have successfully replaced the local branch completely with a remote branch:

We have taught you how to replace the local branch with a remote branch entirely in Git.

Conclusion

To replace the local branch with a remote branch entirely in Git, the user can either rebuild the local branch and replace it with a remote branch or use the “git reset” command. In the first approach, choose the branch you want to reset as remote, and delete it. After that, rebuild the branch and replace it with a remote branch using the “$ git checkout -b master origin/master” command. In the second approach, fetch the remote branches and simply use the “git reset –hard origin/master” command. This write-up has demonstrated how to replace the local branch with a remote branch entirely in Git.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.