44

I've pulled code down from my repo which has messed things up. I'd like to revert my entire project to my last local commit. How would I do this?

Skizit
  • 43,506
  • 91
  • 209
  • 269

3 Answers3

81

This will reset everything to your current commit (getting rid of all changes, staged or otherwise:

git reset HEAD --hard

This will reset everything to the previous commit (also getting rid of all changes, staged or otherwise)

git reset HEAD^ --hard

the ^ next to HEAD means one commit before HEAD, HEAD being where you are currently. You can go two commits back by using ^^, or three with ^^^. Additionally you can use a tilde to specify the number of commits: ~3 for three commits back.

git reset HEAD~3 --hard

Also keep in mind that the --hard option means that these commands will throw away any changes you have that are not stashed.

Chris Rasys
  • 1,295
  • 11
  • 18
  • 4
    It's always nice to see appropriate safety warnings attached to the suggestion to use `git reset --hard`, as in this answer - a worrying number of stackoverflow answers don't... – Mark Longair May 15 '11 at 12:16
  • 1
    Massive caveat of "If you've done a pull since your local commit this will nuke your local commit" :) see this to undo said ball ache http://gitready.com/advanced/2009/01/17/restoring-lost-commits.html – Ed Bishop Nov 17 '15 at 16:11
  • @EdBishop Nice thing about Git is that you nearly never actually lose anything – Chris Rasys Feb 23 '16 at 02:53
  • @ChrisRasys indeed, it's saved us a few times :D – Ed Bishop Feb 24 '16 at 12:37
18

Locate your last local commit in git log and run git reset --hard <commit sha1>.

It will delete all the local changes you haven't commited, and will move the HEAD to this commit.

Community
  • 1
  • 1
Michaël Witrant
  • 7,525
  • 40
  • 44
4

git pull can fetch and merge multiple commits. To go back to your previous local state (rather than back n-commits) you can use the reflog. git reset --hard @{1}

Karl
  • 698
  • 4
  • 4