Tuesday, July 9, 2013

How to revert a single file to a specific previous commit state using git?


For most of developers, I guess we share the same experience
Mon, PM told you to add X feature to your project. Tue, You worked hard to implement it. Wed, You worked hard and finished it. Thu, PM told you he didn't need it any more, so asked you to remove it, restore to your original code.
I don't you, but this sounds my daily experience, especially by the end of our project deadline.

So, as a git user, I found this command to quickly restore your code within a second.
git checkout [commit-ref] -- [filename]
This will revert your file:filename to your specified one im commit:commit-ref.

Let's make an example:
If my file name is MainStoryboard.storyboard, which is located under: projectname/en.lproj/,
then, I'll use
git checkout [commit-ref] projectname/en.lproj/MainStoryboard.storyboard
The commit-ref is easy to find by yourself, while I here will introduce one trick: using master~2, which assumes that you're on the master branch, and the version you want is 2 commits back. You are free to use other branch and another number of commits back to replace [commit-ref], to clarify my word, I'll make an example:
git checkout master~2 projectname/en.lproj/MainStoryboard.storyboard 

No comments :

Post a Comment