Replace local branch with remote branch entirely

3485
Git-Logo-hd

You will some time face situation where you need to completely replace the history of your local master with your remote master. Below are the few methods about how to reset/replace local branch completely with remote branch.

Method-1: Completely overwrite local master with remote branch

One line command to reset local branch with remote branch.

git reset --hard origin/master

This updates your local HEAD branch to be the same revision as origin/master, and --hard will sync this change into the index and workspace as well.

We will require to fetch first for syncing(update your list of remote branches and sync new commits):

git fetch origin remote_branch

Method-2: Renaming local branch and Pull request

This is safest method as you are moving your current staging/un-committed change in separate branch.

  1. Rename your local master branch

git branch -m local-branch local-branch-archive

2.  Create a new master branch from a remote source

git checkout -b local-branch origin/remote-branch

Method-3: Deleting local branch and Pull request

  1. Delete your local branch: git branch -d local_branch
  2. Fetch the latest remote branch: git fetch origin remote_branch
  3. Rebuild the local branch based on the remote one: git checkout -b local_branch origin/remote_branch

 

Facebook Comments