How to revert a file to previous version in Git

Reverting a specific file to a previous version in Git allows you to undo changes to individual files without affecting other modifications. As the creator of CoreUI with over 25 years of development experience, I frequently revert specific files to previous states when debugging or undoing unwanted changes. The most precise approach uses git checkout with a commit hash and file path to restore the file to its exact previous state. This method provides surgical precision for file recovery while preserving other changes in your working directory.

Use git checkout with a commit hash and file path to restore a specific file to its previous version.

git checkout abc123 -- path/to/file.js

This command restores the specified file to its state from commit abc123, leaving all other files unchanged. The -- separator ensures Git treats everything after it as a file path, preventing conflicts with branch names. After running this command, the file will be staged for commit with its previous content, ready to be committed as a revert operation.

Best Practice Note:

This is the file recovery method we use in CoreUI development for precise rollbacks of specific component changes. Use git log --oneline path/to/file.js first to identify the correct commit hash where the file was in the desired state.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author