How to Replace a Local Branch with a Remote Branch in Git
Suppose we have a local branch that we use to develop and a remote branch that we push to.
Let’s say we mess up our local branch in all the worst ways possible.
How can we replace the local branch with the remote branch, so that our changes are gone?
Using git checkout
One option is to simply delete and rebuild the branch.
We can first delete our local branch.
git branch -d local_branch
We can then fetch the remote branch.
git fetch origin remote_branch
Finally, we can checkout that branch locally.
git checkout -b local_branch origin/remote_branch
Using git reset
First, we need to check out the branch we plan on replacing.
git checkout local_branch
Then, we need to perform a reset, where origin/remote_branch
is the remote branch we’re resetting to.
git reset --hard origin/remote_branch
The --hard
flag syncs the change into the index as well as the workspace.
This git reset
command will update our local HEAD
branch to be the same revision as origin/remote_branch
.