Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git reset --hard everything but 4 files

Tags:

git

Assuming nothing is staged, what's the recommended way to reset all the work you've done, except for files:

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

Other dirty files exist in app/models/ and app/views/ that I don't want to keep.

like image 547
Ron Garrity Avatar asked Aug 23 '12 13:08

Ron Garrity


People also ask

Will reset hard remove untracked files?

git reset --hard resets your index and reverts the tracked files back to state as they are in HEAD. It leaves untracked files alone.

Does git reset hard remove new files?

git reset --hard is a classic command in this situation - but it will only discard changes in tracked files (i.e. files that already are under version control). To get rid of new / untracked files, you'll have to use git clean !

How do I completely reset git?

To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).

Does git reset delete files?

Running git reset will typically delete files and commits. And without those, you don't have a project! This is why it's critical to plan ahead when using it, so you don't end up deleting elements that are critical for your project.


1 Answers

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).

like image 124
kan Avatar answered Oct 01 '22 20:10

kan