How to replace local branch with remote branch entirely in Git?
Solution 1:
- 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.
Solution 2:
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
Solution 3:
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.
Solution 4:
git branch -D <branch-name>
git fetch <remote> <branch-name>
git checkout -b <branch-name> --track <remote>/<branch-name>