How to reset a file to an old revision

Resetting a particular file to a specific revision

Hey there! I’m Srebalaji. You are receiving this email because you have subscribed to level up your game in Git.

In this post let’s see different ways of resetting a file to a particular revision. This may come in handy in many situations.

Using checkout

git checkout <commit_hash_id> -- <file_path>

Example
git checkout cc1d4a7 -- example.py

I think the above command is very self-explanatory.

You are reverting a file to a particular commit. In other words, git will take the mentioned file and apply the mentioned commit changes to it.

You can mention multiple files to revert too.

git checkout cc1d4a7 -- example.py index.py

Using restore

git restore --source <commit_hash_id> <file_path>

Example
git restore --source cc1d4a7 index.py

We have seen restore in our previous edition. Restore is a new command and it helps in restoring a particular revision.

In the above command, we are using the —source option to tell restore to use only the particular commit hash. And we have also mentioned the file path to apply the changes only to the mentioned file.

You can use any of the above command to reset a file. My personal favorite is restore.

That’s it for today :)

See you next week :) :)