πŸ“˜ How to revert (restore) only one file to specific commit (SHA)?

Problem

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:

Image

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.

Image

So, how to properly get only one specific old file revision without changing another files in the repository? ?

➑ Resolution

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:

  1. git log --oneline

Image

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)):

Image

But also consider the more convenient method do it using git client, such as GitHub Desktop, because of good diffs viewer:

Image

Anyway, I have only two SHA (revisions) of the my file (plant.png), so current is:

  • bcbf259

And I just need a previous:

  • 1cba3eb

Image

Firstly, I check my repository status and it's clean:

  1. git status

Image

And now I switch only one specific file (plant.png) to the specific revision:

  1. git checkout 1cba3eb Images/plant.png

Image

By checking status (git status) you can see, that only one file changed:

Image

Now you can work with restored version, or right now commit it and push to the server.

Image

If you want hear to return everything as it wasΒ (green fruits), just type (should be before commits; the dot means all):

  1. git restore .

Conclusions


Image

If you use cloud service, like Google Drive, it always upload the new saved version of file, so you could have tens of savings, that hard to figure out which one is significant.

In git VSCΒ you manually set the REVISIONS, so later it could be easy to find the required REVISION.

git checkout typically uses to switch all repo files to the specific commit, but, as we can see, it's not problem to switch only one chosen file to the specific commitΒ ??

Image

β†ͺRECOMMENDATION:

  • Stress test STM32 MCU under high temperature

🌑️ Get temperature from STM32 internal temperature sensor (simple library)

115
No comments yet. Be the first to add a comment!
Cookies?