Skip to content

Instantly share code, notes, and snippets.

@phpfour
Created January 17, 2024 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpfour/300f2cac4f4c7f3c87e50106b3489f6e to your computer and use it in GitHub Desktop.
Save phpfour/300f2cac4f4c7f3c87e50106b3489f6e to your computer and use it in GitHub Desktop.
How to use git to revert a committed file to an earlier version

To revert a committed file to an earlier version using Git, you can use the git checkout command. Here's how you can do it:

git checkout {{commit_hash}} -- {{file_path}}

Replace {{commit_hash}} with the hash of the commit you want to revert to, and {{file_path}} with the path to the file you want to revert.

For example, if you want to revert the file script.js to an earlier version with the commit hash abc123, you would run:

git checkout abc123 -- script.js

This command will replace the contents of script.js with the version from the commit specified by abc123.

Note that this operation will discard any uncommitted changes in the file, so make sure to commit or stash any changes you want to keep before running the git checkout command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment