git: reseting changes

2011-12-12

I've been using git for quite a while now for my own projects. My work flow usually involves getting the project to some meaningful point and committing. After that it often happens that I try something out and pretty soon realize it makes absolutely no sense whatsoever. To get the project back to the state it was after the last commit I've gotten used to do:

$ git reset --hard

What I do more rarely is keep changes to the most of the files, but reset one (or just a few) of them to the state they were at last commit. I keep thinking of this too as “reseting” the file and usually try to command something like “git reset my-source-file.c” and get “fatal: Cannot do hard reset with paths” as a reward. In this case what I should do is checkout the file, e.g.

$ git checkout my-source-file.c

With git checkout out can check files out from older commits or other branches, too, if need be:

$ git checkout HEAD~2 my-source-file.c

For more information refer to git-checkout manual page ;)