3

I made a lot of unhelpful changes, and would like to revert to the state my repo was in before any of the changes.

Is there a git command for doing that?

Thanks!

Trip
  • 26,756
  • 46
  • 158
  • 277

2 Answers2

6

First, to revert changes to tracked files:

git reset --hard HEAD

git reset alone resets the index; adding --hard resets the working copy as well. If you've already committed, specify a different commit to reset to - eg, HEAD^ to revert to the parent commit of HEAD (ie, to remove the latest commit).

Next, to delete all untracked files:

git clean -dfx

-d tells it to delete directories, -f forces it to actually do the delete, and -x skips .gitignored files.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • After performing `git reset --hard HEAD` I'm still left with three of these `# modified: vendor/plugins/human_attribute_override (modified content)` – Trip May 21 '11 at 01:10
  • In theory `--hard` ought to do it, but if that doesn't work, try `git checkout -f` as well. – bdonlan May 21 '11 at 01:11
  • Ah `git checkout -f` didn't not resolve those three little boogers either. – Trip May 21 '11 at 01:16
  • Interesting - are they read-only or something? `git reset --hard` should blow away any modified tracked files; if it doesn't, and there's no error, it's a bug. Try the git mailing list I guess... – bdonlan May 21 '11 at 01:18
  • 1
    Thanks Brother! I appreciate it. The three files in question are actually directories.. Strange. – Trip May 21 '11 at 01:29
  • 1
    Ahh! its because they are submodules.. not sure how to deal with those though. – Trip May 21 '11 at 01:31
  • @Trip, ah, I've not worked with submodules much, I'm afraid :/ – bdonlan May 21 '11 at 01:32
  • Each submodule is its own repo, you'll have to work with them individually the same way or wipe them out and redo the `git submodule init` stuff. – Tekkub May 21 '11 at 01:59
3

If I understand correctly you have committed something and want it reverted git reset --hard HEAD^

If you haven't committed anything and it's only your working tree that is messed up then git reset --hard HEAD