Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
2 views
in DevOps and Agile by (19.4k points)
edited by

I have two branches:

  1. local branch (the one which I work with)
  2. remote branch (public, only well-tested commits go there)

Recently I seriously messed up my local branch.

How would I replace the local branch entirely with the remote one, so I can continue my work from where the remote branch is now?

I have already searched and checked out to the remote branch locally does not have any effect.

2 Answers

+3 votes
by (27.5k points)

Step 1: Delete your local branch

$ git branch -d local_branch

Step 2: Fetch the latest remote branch

$ git fetch origin remote_branch

Step 3: Rebuild the local branch based on the remote one

$ git checkout -b local_branch origin/remote_branch

How to replace local with remote branch entirely in Git?
Intellipaat-community
by (29.3k points)
Awesome! Great answer.
0 votes
by (2.8k points)

Remove the Local Branch:

git branch -d local_branch

This delete command is your tool to remove or uproot your branch. If your branch stays apart and even has some local changes, use -D to force it to delete:

git branch -D local_branch

Get the Most Current Version of the Remote Branch:

git fetch origin remote_branch

This is something that tells you that you have the latest copy of the remote one in your local repo.

Checkout to Origin and Make a New Local Branch through the Remote Branch:

git checkout -b local_branch origin/remote_branch

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...