NeuroAgent

Git File Revert: Complete Guide to Specific Revisions

Learn how to reset or revert files to specific Git revisions using git checkout, git restore, and git revert commands. Complete guide with examples and best practices for file reversion in Git.

Question

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)?

NeuroAgent

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

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:

bash
# 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:

bash
git checkout <commit-hash> -- path/to/file

For example, if you want to revert the file README.md to the version in commit 9cbe84d:

bash
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 checkout command 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:

bash
git restore --source=<commit-hash> -- path/to/file

For example:

bash
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 checkout functionality
  • Defaults to restoring from HEAD when no source is specified

According to the official Git documentation, git restore is designed to be a more intuitive replacement for some git checkout operations.

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:

bash
git revert <commit-hash> --no-commit -- <file-path>

For example:

bash
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

bash
# 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

bash
# 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:

bash
# Restore multiple files from different commits
git checkout commit1 -- file1.js
git checkout commit2 -- file2.js
git checkout commit3 -- file3.js

Safety Considerations

  1. Always review changes before committing:

    bash
    git diff
    git status
    
  2. Use --staged for staged files:

    bash
    git restore --staged --source=<commit> -- path/to/file
    
  3. Consider branching before making significant changes:

    bash
    git 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

  1. How can I reset or revert a file to a specific revision? - Stack Overflow
  2. How to Reset or Revert a File to a Specific Revision in Git? - GeeksforGeeks
  3. Git Revert File to Previous Commit: How to Do It? - GitProtect.io
  4. How Can I Reset or Revert a File to a Specific Revision? - Better Stack Community
  5. Resetting, Checking Out & Reverting - Atlassian Git Tutorial
  6. Git - git-restore Documentation
  7. Reverting A File To Previous Commit - GeeksforGeeks
  8. 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.