[git] Overwriting my local branch with remote branch

I have completely fubar'd my local branch, and would like to start over. The version on the server is correct.

I don't want to start over, I would like to use my local history to fix my huge screwup. (I can if I have to.)

git fetch branchname, and git pull branchname don't work. The message I get is "up to date" however, my local version does not match that of the server.

git pull origin/branchname gives me a "not found" error.

This question is related to git

The answer is


first, create a new branch in the current position (in case you need your old 'screwed up' history):

git branch fubar-pin

update your list of remote branches and sync new commits:

git fetch --all

then, reset your branch to the point where origin/branch points to:

git reset --hard origin/branch

be careful, this will remove any changes from your working tree!


What I do when I mess up my local branch is I just rename my broken branch, and check out/branch the upstream branch again:

git branch -m branch branch-old
git fetch remote
git checkout -b branch remote/branch

Then if you're sure you don't want anything from your old branch, remove it:

git branch -D branch-old

But usually I leave the old branch around locally, just in case I had something in there.


Your local branch likely has modifications to it you want to discard. 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.

Note: always do this on a backed-up repo. That way you can assure you're self it worked right. Or if it didn't, you have a backup to revert to.


git reset --hard

This is to revert all your local changes to the origin head