How to revert only one file back to a revision or commit

I messed up with one file in one repository and want to check out only that file back to a commit. How to achieve this?

To check the file with my_file.name at git commit commit_sha1:

$ git checkout commit_sha1 my_file.name

You can also use the relative commit instead of commit_sha1 such as master~4, HEAD~4, HEAD^, master^ and etc.

To check back (the master) of myfile.name:

$ git checkout master my_file.name
Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

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