loading...

How to reset a file to an old revision

srebalaji profile image Srebalaji Thirumalai Originally published at gitbetter.substack.com on ・1 min read

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

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 commands to reset a file. My personal favorite is restore.

Thank you for reading :)

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

Discussion

pic
Editor guide