📘 How to revert (restore) only one file to specific commit (SHA)?
1) Currently I have git repository with green fruits image (could be any file type), where I earlier added a several changes and commit and push them to the server:
But then, I realize, that I don't need that changes (green fruits) and I have the previous version, that I need (yellow fruits) in git repository history.
So, how to properly get only one specific old file revision without changing another files in the repository? ?
So, you need to find commit where your file revision was that you want (not a commit with last changes of this file), you can do this by right-click in repository folder, press Git Bash Here, and to show full history type and enter:
- git log --oneline
You can do this by look at the history of the specific file (in my case it's .png image, that placed in Images folder (see the explorer)):
But also consider the more convenient method do it using git client, such as GitHub Desktop, because of good diffs viewer:
Anyway, I have only two SHA (revisions) of the my file (plant.png), so current is:
And I just need a previous:
Firstly, I check my repository status and it's clean:
- git status
And now I switch only one specific file (plant.png) to the specific revision:
- git checkout 1cba3eb Images/plant.png
By checking status (git status) you can see, that only one file changed:
Now you can work with restored version, or right now commit it and push to the server.
If you want hear to return everything as it was (green fruits), just type (should be before commits; the dot means all):
- git restore .