Sometimes, you may need to revert or checkout a file to its state at a previous commit without disturbing other files or the current project state. This guide will walk you through various methods to checkout, revert, or reset a specific file to a previous commit's state in Git.
Understanding the commands
- git checkout: This command is used to switch branches or restore working tree files. It can also be used to checkout files from a specific commit to your working directory.
- git revert: This command is used to reverse changes that were made by previous commits. It can target specific commits and is safe for use in public branches because it preserves the history.
- git reset: This command is used to reset the index and working directory to a specific state. If you need to undo changes in your staging area and working directory,
git reset
can be applied to specific files.
How to checkout a file from a previous commit
If you want to restore a file from a previous commit, you can use the git checkout
command.
Step-by-step guide
Identify the commit SHA: First, find the commit SHA (a unique identifying code for each commit) of the version of the file you want to restore. You can find this by using:
Terminalgit log -- path/to/fileThis command will show a list of commits that affected the specified file. Note the SHA of the commit from which you want to restore the file. This will show you the commit history of the specified file.
Checkout the file: Once you have the commit SHA, use the following command to checkout the file:
Terminalgit checkout <commit-sha> -- path/to/fileReplace
<commit-sha>
with the actual SHA of the commit andpath/to/file
with the actual path to the file.
This command will restore the file to its state at the specified commit without changing the current branch or affecting other files.
Reverting changes for one file
Sometimes, instead of checking out to a previous commit, you might want to revert the changes made to a file in a specific commit.
How to revert changes from a specific commit
- Use
git revert
with the commit SHA: If you want to undo the changes made to a file in a specific commit and this commit only changed that one file, you can run:If the commit includes changes to multiple files and you only want to revert the changes made to one file:Terminalgit revert <commit-sha>Terminalgit revert --no-commit <commit-sha>git reset HEADgit add path/to/filegit commit -m "Reverted changes to path/to/file"
This method will create a new commit on your current branch, reverting the changes made to the specified file.
Resetting files from a commit
If you want to undo changes to a file that you haven't committed yet or remove changes from the staging area, you can use the command git reset
.
How to reset changes to a file from a commit
Reset a specific file:
Terminalgit reset <commit-sha> -- path/to/fileThis command will unstage the file from the specified commit but keep the changes in your working directory. From here you can make safely make further changes to the file.
Discard changes completely:
Terminalgit checkout HEAD -- path/to/fileThis is useful if you want to discard changes to a file and revert it to the state it was in at the last commit. Use caution as this is a potentially destructive command, as it will discard your local changes.
Summary
For more information on checking out a file from a previous commit, see the official Git documentation.