15

I messed my local branch of a project, and i want to revert my local copy to the remote state. How i acomplish that simply goal?

LuisEspinoza
  • 8,508
  • 6
  • 35
  • 57
  • Possible duplicate of [Reset local repository branch to be just like remote repository HEAD](http://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head) – Michael Freidgeim Nov 20 '15 at 01:12

4 Answers4

23

If you are not afraid of losing any local history, you can switch to another branch then delete your local branch, then check the remote version out. For example, if you wanted to revert a branch called "test_feature," you could do this:

$ git checkout master
$ git branch -D test_feature # see note about -D below
$ git checkout test_feature  # should track origin/test_feature

NOTE: -D will force delete the branch, and will suppress warnings about unmerged changes.

This is useful if you have merged a branch you didn't intend to, as the HEAD pointer can change depending on the type of merge.

EDIT: Another method for doing the same thing is to simply type:

git reset --hard origin/test_feature

This will reset the branch you are currently on to the state of the remote (origin in this case) branch test_feature.

@hvgotcodes has a variation of this in his example (he's targeting the HEAD commit)

zedd45
  • 2,101
  • 1
  • 31
  • 34
  • 1
    If you happen to have the remote branced with the same name, for example `forked_repo/feature` then the reset syntax needs to include `remotes/` to avoid ambiguity. `git reset --hard remotes/forked_repo/feature` – edA-qa mort-ora-y Nov 30 '17 at 09:03
  • That's what `origin` refers to in this case; whatever your remote name is. Most people will be origin if they simply clone, but if you are forking, you could have multiple remotes, so this is key for people forking. – zedd45 Oct 06 '21 at 15:39
5

you can reset with

git reset --hard HEAD

you will lose all your changes if you do this. This might be helpful.

zedd45
  • 2,101
  • 1
  • 31
  • 34
hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • i try it, but my project no seems to revert to remote branch state and when i execute git status, i receive the output: # On branch TestColumnsNews # Your branch is ahead of 'origin/TestColumnsNews' by 4 commits. # nothing to commit (working directory clean) – LuisEspinoza Mar 21 '12 at 19:49
  • it says that my branch is ahead by 4 commits!...why? – LuisEspinoza Mar 21 '12 at 19:50
  • http://stackoverflow.com/q/2389361/1014773 this was what i exactly did and resolve my problem, but your answer lead me in the right direction, thanks. – LuisEspinoza Mar 21 '12 at 19:59
  • 3
    This answer do not answer the stated question. – dentarg Sep 24 '12 at 07:51
  • After this command: apc-2:rmr2 antonio$ git status # On branch master # Your branch and 'origin/master' have diverged, # and have 3 and 326 different commits each, respectively. How is this the correct solution? – piccolbo Mar 14 '13 at 18:19
2

If you want to revert to remote LAST version:

git fetch --all
git reset --hard origin/master

If you want to revert to a specific version:

First, get the string that identify the commit in some date, doing:

git rev-list -n 1 --before="2009-07-27 13:37" origin/master

it prints the commit identifier, take the string (for instance XXXX) and do:

git checkout XXXX
Luca C.
  • 11,714
  • 1
  • 86
  • 77
0

There's simple one liners to do what you're asking, but usually when I feel like I've "messed up a branch" vs. what's on the remote, I use interactive rebasing, which gives me a bit more control. That way I can edit commits if need be, or remove lines if I just don't want the commit. See here: http://book.git-scm.com/4_interactive_rebasing.html

dnmcgoy
  • 1
  • 1