Wednesday, April 20, 2011

How would you go about reverting a single file to previous commit state using git?

I wonder why isn't it implemented in the GUI? I know git has a content approach vs. single file approach but sometimes single file reverting is crucial.

For instance an .png graphics file from a few commits before is needed.

Probably I need to do a checkout but I'm still not sure as the concepts sometimes escape me.

From stackoverflow
  • If you know how many commits back you need to go, you can use:

    git checkout master~5 image.png
    

    This assumes that you're on the master branch, and the version you want is 5 commits back.

    hallidave : You can use HEAD~5 rather than master~5 if you're not on the master branch.
    dylanfm : Am I right to think that HEAD^ or master^ goes back just 1 commit?
    Ron DeVera : @dylanfm, that's right.
  • To expand on Ron DeVera's post, where he mentions master~5, you can use any reference to a git commit, including the SHA-1 if that's most convenient. The point is that the command looks like this:

    git checkout [commit-ref] [filename]

    keepyourliberty : Thanks that's useful especially if one doesn't want look for branch names or count how many commits etc.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.