How can I reset or revert a file to a specific revision in Git?
How can I revert a modified file to its previous revision at a specific commit hash (which I determined via git log and git diff)?
To reset or revert a file to a specific revision in Git, you can use several approaches depending on your Git version and specific requirements. The most common methods involve using git checkout, git restore, or git revert with appropriate parameters.
Contents
- Getting Started: Finding the Commit Hash
- Using git checkout to Revert a File
- Using git restore for File Reversion
- Using git revert for Specific Files
- Understanding Command Differences
- Practical Examples and Best Practices
Getting Started: Finding the Commit Hash
Before you can revert a file to a specific revision, you need to identify the commit hash you want to revert to. Use these commands to find the appropriate commit:
# View commit history for a specific file
git log --oneline --follow path/to/file
# Show differences between commits
git diff <commit-hash> HEAD -- path/to/file
The git log command shows the commit history for the specified file, allowing you to find the exact commit hash where the file had the desired state.
Using git checkout to Revert a File
The traditional approach to revert a file to a specific commit uses git checkout:
git checkout <commit-hash> -- path/to/file
For example, if you want to revert the file README.md to the version in commit 9cbe84d:
git checkout 9cbe84d -- README.md
This command:
- Restores the file from the specified commit
- Places the file in your working directory
- Does not move the HEAD position
- Does not create a new commit
Note: According to GeeksforGeeks, the
git checkoutcommand is widely used for this purpose and has been available in Git for a long time.
Using git restore for File Reversion
For Git versions 2.23 and newer, git restore is the recommended modern approach:
git restore --source=<commit-hash> -- path/to/file
For example:
git restore --source=9cbe84d -- README.md
The git restore command:
- Is more explicit and easier to understand
- Has clearer option names
- Is the modern replacement for some
git checkoutfunctionality - Defaults to restoring from HEAD when no source is specified
According to the official Git documentation,
git restoreis designed to be a more intuitive replacement for somegit checkoutoperations.
Using git revert for Specific Files
While git revert is typically used to undo entire commits, it can also target specific files with the --no-commit flag:
git revert <commit-hash> --no-commit -- <file-path>
For example:
git revert 9cbe84d --no-commit -- README.md
This approach:
- Creates a revert commit but doesn’t immediately commit it
- Only reverts changes for the specified file
- Gives you a chance to review the changes before committing
- Preserves the history by creating a new commit
Important: As noted in the GitProtect.io blog, this method reverts changes for the specified file without committing straight away, allowing you to review and commit manually.
Understanding Command Differences
The three main commands serve different purposes:
| Command | Purpose | Scope | History Impact |
|---|---|---|---|
| git checkout | Restores file from commit | File-specific | No new commit |
| git restore | Modern file restoration | File-specific | No new commit |
| git revert | Creates undo commit | Can be file-specific | Creates new commit |
As Atlassian’s Git Tutorial explains, “git reset tells Git to move HEAD to a different commit. git checkout on the other hand doesn’t ask Git to do anything with HEAD at all.”
Practical Examples and Best Practices
Basic File Reversion
# Find the commit hash
git log --oneline --follow src/main.js
# Revert to specific commit
git checkout a1b2c3d -- src/main.js
# Add and commit the change
git add src/main.js
git commit -m "Revert src/main.js to previous version"
Interactive Restoration
# Restore from specific commit without committing immediately
git restore --source=9cbe84d -- config/database.yml
# Review the changes
git diff
# If satisfied, commit
git add config/database.yml
git commit -m "Restore database configuration to previous state"
Batch File Restoration
You can restore multiple files from different commits:
# Restore multiple files from different commits
git checkout commit1 -- file1.js
git checkout commit2 -- file2.js
git checkout commit3 -- file3.js
Safety Considerations
-
Always review changes before committing:
bashgit diff git status
-
Use --staged for staged files:
bashgit restore --staged --source=<commit> -- path/to/file -
Consider branching before making significant changes:
bashgit checkout -b revert-branch # Perform reversion operations # Test before merging
As Better Stack Community advises, “Resetting a file changes its content in your working directory to match the specified commit, while reverting creates a new commit that undoes changes.”
Sources
- How can I reset or revert a file to a specific revision? - Stack Overflow
- How to Reset or Revert a File to a Specific Revision in Git? - GeeksforGeeks
- Git Revert File to Previous Commit: How to Do It? - GitProtect.io
- How Can I Reset or Revert a File to a Specific Revision? - Better Stack Community
- Resetting, Checking Out & Reverting - Atlassian Git Tutorial
- Git - git-restore Documentation
- Reverting A File To Previous Commit - GeeksforGeeks
- How to Revert a File to a Previous Commit in Git - Delft Stack
Conclusion
Reverting a file to a specific revision in Git can be accomplished using several methods:
- Use
git checkout <commit> -- <file>for traditional file reversion - Use
git restore --source=<commit> -- <file>for modern Git versions (2.23+) - Use
git revert <commit> --no-commit -- <file>when you want to create a revert commit
The best approach depends on your Git version and whether you need to preserve history. For most cases, git restore is the recommended modern approach, while git checkout remains widely supported and effective. Always review changes with git diff before committing to ensure you’re getting the expected result.
Remember that these commands only affect the working directory - you’ll need to stage and commit the changes if you want to make them permanent in your repository.