How to git reset --hard everything but 4 files

One way is to git commit those four files, then reset hard, and then undo the commit:

git add <files to keep>
git commit -m "temp"
git reset --hard
git reset HEAD~

If those four files are in the index while the dirty files aren't, a simple git-clean should do the trick. If unsure, use the --dry-run switch first, and notice the --exclude switch.

Otherwise, stick with siride's answer


As you described the problem, it doesn't look like you want do reset. The simplest thing for this is:

 git add app/models/a.rb app/views/a/index.html.rb config/foo.rb config/bar.rb
 git checkout .

so, it adds your 4 files into the index, and checks out clean versions (i.e. discards changes) for other files.

I assume you have not staged (git add) modified files before (you should then unstage it using reset).

Tags:

Git