How to replace local branch with remote branch entirely in Git?
- Make sure you've checked out the branch you're replacing (from Zoltán's comment).
Assuming that master is the local branch you're replacing, and that "origin/master" is the remote branch you want to reset to:
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.
git branch -D <branch-name>
git fetch <remote> <branch-name>
git checkout -b <branch-name> --track <remote>/<branch-name>
I'm kind of surprised no one mentioned this yet; I use it nearly every day:
git reset --hard @{u}
Basically, @{u}
is just shorthand for the upstream branch that your current branch is tracking. For example, this typically equates to origin/[my-current-branch-name]
. It's nice because it's branch agnostic.
Make sure to git fetch
first to get the latest copy of the remote branch.
Side Note: if you're using Git in PowerShell you'll need to escape the special characters first, though I've also confirmed it works as a string, so for example either of these will work:
git reset --hard `@`{u`}
# or
git reset --hard "@{u}"
That's as easy as three steps:
Delete your local branch:
git branch -d local_branch
Fetch the latest remote branch:
git fetch origin remote_branch
Rebuild the local branch based on the remote one:
git checkout -b local_branch origin/remote_branch