Search code examples
gitgit-revert

How to get older version of a file in git


Suppose my last commit includes 6 files A, B, C, D, E, F. Now I only want to revert changes of only file B.

I have committed my whole project in git. My commit id is 41e6150. There are many files in my commit after the last commit but I want to only revert changes of specific file say X. how can I do that?


Solution

  • Probably your best option is to checkout the old version of that file and then create a new commit:

    Suppose your old commit was 1a2b3c:

    git checkout 1a2b3c -- fileX
    

    Now you have a new status where all files are up to date but fileX is as it was at 1a2b3c. Just create a new commit and you've done.