I have two branches:
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 SO and checking out to the remote branch locally does not have any effect.
If you need to completely replace the history of your local master with your remote master (for example, if you've been playing with commits or rebase), you can do so by renaming your local master, and then creating a new master branch.
Just like git push --force allows overwriting remote branches, git fetch --force (or git pull --force ) allows overwriting local branches. It is always used with source and destination branches mentioned as parameters.
Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With