git: revert a single file to a specific revision

Turns out to be quite easy:

  1. Identify the hash of the revision you want to revert to, e.g. abc123
  2. At the command prompt, type:

git checkout abc123 path/to/file

Why would I need to do this? Don’t ask :) But I definitely took something to heart: git positively encourages you to create branches, and I don’t do it enough. If I had made a branch for the single feature I had a problem with, instead of rolling it into a general “development” branch, I wouldn’t have been at the computer at 9:30 pm tearing my hair out trying to roll back individual files! Lesson learned. But I do one thing right: atomic commits. Make sure each commit relates to a single feature, then at least it’s easy to identify affected files. Commits of unrelated fixes are a Bad Thing.

This entry was posted in Web development and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>