To revert a single file to a previous version in Git, you can use the git checkout or git restore command.
Method 1: Using git checkout (older method)
git checkout
Replace
Replace path/to/your/file with the relative path of the file.
For example:
git checkout abc1234 — src/main.py
This will restore main.py to the state it was in at commit abc1234, but it does not change the commit history.
Method 2: Using git restore (recommended)
git restore –source=
Replace
Replace path/to/your/file with the relative path of the file.
For example:
git restore –source=abc1234 src/main.py
This method is newer and more explicit for restoring files in Git.
After Reverting:
If you want to keep this change, stage and commit the file:
git add path/to/your/file
git commit -m “Revert file to previous version”
This will commit the reverted version of the file.