How to replace local branch with remote branch entirely in Git?

git delete local branch
git checkout remote branch
git update local branch from remote
git replace remote branch with local
git delete remote branch
git pull overwrite local repo
git pull remote branch
git update local branch from remote master

I have two branches:

  1. local branch (the one which I work with)
  2. remote branch (public, only well-tested commits go there)

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.

  1. Make sure you've checked out the branch you're replacing (from Zoltán's comment).
  2. 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.

How to replace local with remote branch entirely in Git?, Method-1: Completely overwrite local master with remote branch. One line command to reset local branch with remote branch. git reset --hard origin/master. Step 1: Delete your local branch $ git branch -d local_branch. Step 2: Fetch the latest remote branch $ git fetch origin remote_branch. Step 3: Rebuild the local branch based on the remote one $ git checkout -b local_branch origin/remote_branch

That's as easy as three steps:

  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

Replace local branch with remote branch entirely, If you feel the need to discard all your local changes and just reset/overwrite everything with a copy from the remote branch then you should� 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>

Git Pull - How to Override Local files with Git Pull - Guide, How to completely replace one branch's code and git history with another (old- master) git branch -m master # Sync local master with remote� How to completely replace one branch’s code and git history with another. Here’s how I did it for our team: # Rename master to old-master, staging to master $ (staging) git checkout master $ (master) git branch -m old-master $ (old-master) git checkout staging $ (staging) git branch -m master # Force push staging (now master) into remote master $ (master) git push origin master -f # Change branch names back to original $ (master) git branch -m staging $ (staging) git checkout old-master

Replace everything with the remote branch; but, only from the same commit your local branch is on:

git reset --hard origin/some-branch

OR, get the latest from the remote branch and replace everything:

git fetch origin some-branch
git reset --hard FETCH_HEAD

As an aside, if needed, you can wipe out untracked files & directories that you haven't committed yet:

git clean -fd

How to force “git pull” to overwrite a local branch?, #Rename your local master branch git branch -m master _old_master_branch_ # Create a new master branch from a remote source git� In your specific question, you want to keep all of the remote files and replace the local files of the same name. Replace conflicts with the remote version. git merge -s recursive -Xtheirs upstream/master will use the remote repo version of all conflicting files. Replace conflicts with the local version. git merge -s recursive -Xours upstream/master

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.

How to completely replace git branch code with another branch's , Remote-tracking branches are references to the state of remote branches. Git also gives you your own local master branch starting at the same place as to a remote branch you just pulled down, or want to change the upstream branch you' re If you want totally up to date ahead and behind numbers, you'll need to fetch � You can rename/remove master on remote, but this will be an issue if lots of people have based their work on the remote master branch and have pulled that branch in their local repo. That might not be the case here since everyone seems to be working on branch 'seotweaks'. In that case you can: git remote --show may not work.

Completely overwrite local master with remote (Example), git stash git pull. . Together, these commands will discard your local changes for later) and replace them with the latest commit from your remote branch. Git at your fingertips, there's almost never a good reason to completely delete code. To do this, you'll need to use git reset to reset the branch head to the last spot that you diverged from the upstream repo's branch. Use git branch -v to find the sha1 id of the upstream branch, and reset your branch it it using git reset SHA1ID. Then you should be able to do a git checkout to discard the changes it left in your directory.

Remote Branches, I have two branches: local branch (the one which I work with) remote branch ( public, only well-tested commits go there) Recently I seriously� create a new remote, fetch it into your local so your local git knows about its branches and all, create a new branch from the remote branch and checkout to that. Now if you want to publish this new local branch to your remote and set the upstream url also

"Git force pull", git branch names are just pointers, so renaming staging to master and doing a git push origin master will update remote's master force pushing a branch to a remote will force the remote branch to

Comments
  • I know the accepted answer has 1280 up-votes, but you should really consider changing the accepted answer to the one by @TTT.
  • Thanks for your suggestion, I am just so 'scared' of using --hard and --force already, so I just picked the solution which does not use those.
  • @KonstantinLevin: ah yes, the naming of those options is rather irritating. git reset by default will repoint your current branch and sync the index. --soft will skip updating the index, --hard will also sync the workspace. My own experience is using --hard most of the time, except when I want to undo the last commit (which is just git reset HEAD^)
  • After having more experience with git I'm convinced this is a better solution, thanks.
  • probably you will need to fetch first: git fetch origin remote_branch
  • You should note that this will replace whichever branch your currently on with the contents of master. So if you're on e.g. a feature-branch, it will replace all its commits with master, so make sure you've checked out the branch you're replacing first.
  • Actually what @araqnid said is right and more concise. I've tested it and you may try it too.
  • Wow, the git checkout -b local_branch origin/remote_branch is great! I always did this in two seperate commands. thanks!
  • You might need to do git branch -D local_branch in the first step if your branch is not merged.
  • thanks, I had a hard time when using gitflow, after publishing a branch and then finishing it, i wanted to go to the deleted branch, and your solution was the only one that worked, pull does not seem to work.. or I mave have not use it well --
  • we should ensure that the current branch is not the one which is to be to deleted.
  • What does the --track part do?