views:

13855

answers:

3

I have made some changes to a file which has been committed in a few times as part of a group of files, but now want to reset/revert the changes on it back to a previous version.

I have done a git log along with a git diff to find the revision I need, but just have no idea how to get the file back to its former state in the past.

Any help is greatly appreciated.

+40  A: 

Assuming the commit you want is abcde:

git checkout abcde file/to/restore

The git checkout man page gives more information.

As a side note, I've always been uncomfortable with this command because it's used for both ordinary things (changing between branches) and unusual destructive things (discarding changes in the working directory).

Greg Hewgill
The git command set is a bit messed up. Ask a new user what they think "revert" does - it sure wasn't what I thought
1800 INFORMATION
This doesn't work for me
Milan Babuškov
Thanks, this was really helpful.
Mattew
Saved my ass. Thank you.
tester
To be clear, if you are on the `master` branch, it is: `git checkout master file/to/restore`.
Sam Soffes
@Sam Soffes: The `abcde` in my answer represents the SHA1 of the specific commit as requested in the question. Using `master` there also names a specific commit, which is the most recent one on the `master` branch.
Greg Hewgill
Oh good call. That's rather handy.
Sam Soffes
+2  A: 

I have to plug EasyGit here, which is a wrapper to make git more approachable to novices without confusing seasoned users. One of the things it does is give more meanings to git revert. In this case, you would say:

eg revert --since your_previous_commit foo/bar foo/baz

Aristotle Pagaltzis
+7  A: 

I had the same issue just now and I found this answer easiest to understand (commit-ref is the SHA value of the change in the log you want to go back to):

git checkout [commit-ref] [filename]

This will put that old version in your working directory and from there you can commit it if you want.

bbrown