How to Revert a File to a Specific Revision in Git

Quick Answers: Reverting a File to a Specific Revision

  1. Revert a File Using git checkout (Git < 2.23):
   git checkout <commit-hash> -- path/to/file
  1. Revert a File Using git restore (Git 2.23+):
   git restore --source=<commit-hash> -- path/to/file
  1. Commit the Reverted File:
   git add path/to/file
   git commit -m "Revert file to previous version"
  1. Find the Commit Hash:
   git log --oneline path/to/file

Introduction

When working with Git, you may need to revert a file to a specific revision to undo changes, recover a previous version, or fix mistakes. For example, you might want to restore a file to how it was at a particular commit without affecting other files or the entire repository. Git provides straightforward commands like git checkout or git restore to achieve this. In this guide, we’ll walk you through how to revert a file to a specific revision in Git, with clear examples and best practices. Whether you’re a beginner or managing complex repositories, this post will help you restore files confidently.

Why Revert a File to a Specific Revision?

Reverting a file to a specific revision is useful for several reasons. For instance, you might need to:

  • Undo unwanted changes: Restore a file to a stable version after accidental edits.
  • Recover deleted content: Retrieve a file’s state from an earlier commit.
  • Debug issues: Test a file’s behavior at a specific point in history.
  • Prepare fixes: Revert a file before applying new changes or creating a patch.

By targeting a single file, you maintain the rest of your repository’s state, keeping your workflow focused.

Detailed Explanation: Reverting a File to a Specific Revision

To revert a file to a specific revision, you’ll retrieve its content from a past commit and stage it for a new commit. Here’s how to do it step-by-step.

Step 1: Find the Desired Commit

Identify the commit containing the version of the file you want to restore.

  • Command:
  git log --oneline path/to/file
  • Example Output:
  abc123 Update config.json
  def456 Add error handling
  ghi789 Initial config setup

Suppose you want config.json from ghi789.

Step 2: Revert the File to the Specific Revision

Use either git checkout (older Git versions) or git restore (Git 2.23+) to restore the file’s content from the chosen commit.

  • Using git checkout (Git < 2.23):
  git checkout <commit-hash> -- path/to/file

Example:

  git checkout ghi789 -- config.json
  • Using git restore (Git 2.23+):
  git restore --source=<commit-hash> -- path/to/file

Example:

  git restore --source=ghi789 -- config.json
  • What it does:
  • Copies the file’s content from the specified commit to the working directory and index (staging area).
  • Leaves other files unchanged.
  • Note: The file is now staged for a commit.
Step 3: Commit the Reverted File

Create a new commit to save the reverted file.

  • Commands:
  git add path/to/file
  git commit -m "Revert config.json to commit ghi789"
  • Example:
  git add config.json
  git commit -m "Restored config.json to initial setup"
Step 4: Verify the Change

Confirm the file matches the desired revision.

  • Command:
  git log --oneline
  cat path/to/file
  • Optional: Compare with the original commit:
  git show ghi789:path/to/file

Alternative: Revert Without Committing Immediately

If you only want to inspect the file’s previous state without staging it:

  • Using git show:
  git show <commit-hash>:path/to/file > path/to/file

Example:

  git show ghi789:config.json > config.json
  • What it does: Overwrites the file in the working directory but doesn’t stage it.
  • Use case: Testing or manual edits before committing.

Practical Examples

  1. Restore a Configuration File:
   git log --oneline settings.py
   # Find commit def456
   git restore --source=def456 -- settings.py
   git add settings.py
   git commit -m "Reverted settings.py to def456"
  1. Recover a Deleted File:
   git log --oneline --diff-filter=D --summary  # Find deleted file’s last commit
   git checkout abc123 -- deleted-file.txt
   git commit -m "Restored deleted-file.txt"
  1. Inspect Before Committing:
   git show ghi789:config.json > config.json
   # Edit or review config.json
   git add config.json
   git commit -m "Updated config.json from ghi789"

Best Practices for Reverting Files

  • Verify the commit: Use git log or git show <commit-hash>:path/to/file to ensure the correct revision.
  • Backup changes: Stash or commit uncommitted changes before reverting:
  git add .
  git stash
  • Use git restore for modern Git: It’s clearer than git checkout for file restoration (Git 2.23+).
  • Document the revert: Write descriptive commit messages to explain why the file was reverted.
  • Test after reverting: Ensure the reverted file works as expected in your project.
  • Avoid force-pushing unless necessary: If the branch is shared, coordinate with your team before rewriting history.

Common Pitfalls and How to Avoid Them

  • Choosing the wrong commit: Double-check the commit hash with git log or git show.
  • Overwriting uncommitted changes: Stash or commit changes first to avoid losing work.
  • Confusing checkout and restore: Use git restore in Git 2.23+ for clarity; checkout can also switch branches.
  • Forgetting to commit: Stage and commit the reverted file to save the change.
  • Shared branches: If the file’s history is pushed, avoid rewriting public history without team approval.

Conclusion

To revert a file to a specific revision in Git, commands like git restore or git checkout make the process simple and precise. By identifying the desired commit, restoring the file, and committing the change, you can recover previous versions without affecting other files. For example, whether you’re fixing a bug or recovering lost code, these steps ensure a clean workflow. Therefore, with these best practices, you’ll manage your Git repository with confidence and keep your file history organized.

Got a Git question or a file reversion tip? Share it in the comments or explore our Git tutorials for more version control insights!

Call to Action

Loved this guide? Subscribe to our newsletter for more Git tips and tricks, or check out our version control resources to level up your skills. Let’s keep your Git history clean and efficient!


Previous Article

How to Execute a Program or Call a System Command in Python

Next Article

How to Use a Tap by Location Helper for Unresponsive Elements in Appium

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨