How can I revert a single file to a previous version?

Learn how can i revert a single file to a previous version? with practical examples, diagrams, and best practices. Covers git, revert development techniques with visual explanations.

How to Revert a Single File to a Previous Version in Git

Hero image for How can I revert a single file to a previous version?

Learn various methods to restore a single file in your Git repository to an earlier state, whether it's committed or uncommitted.

Git is a powerful version control system that allows you to track changes to your files and revert them if necessary. While reverting an entire commit is a common operation, sometimes you only need to restore a single file to a previous state without affecting other changes in your working directory or repository history. This article will guide you through different scenarios and commands to achieve this, ensuring you can precisely control your codebase.

Understanding Git's File States

Before diving into the commands, it's crucial to understand the different states a file can be in within Git:

  • Working Directory: The files you are currently editing.
  • Staging Area (Index): Files that are marked to be included in the next commit.
  • Local Repository (HEAD): The last committed version of your files on your current branch.
  • Remote Repository: The version of your files on a remote server (e.g., GitHub, GitLab).
graph TD
    A[Working Directory] --> B{git add}
    B --> C[Staging Area]
    C --> D{git commit}
    D --> E[Local Repository (HEAD)]
    E --> F{git push}
    F --> G[Remote Repository]
    E -- "git checkout <file>" --> A
    C -- "git reset <file>" --> A
    E -- "git restore --source <commit> <file>" --> A

Git file states and common commands affecting them

Scenario 1: Reverting an Uncommitted File

If you've made changes to a file in your working directory but haven't yet staged or committed them, you can easily discard these changes and restore the file to its last committed state (or its state in the staging area if it was staged). This is often referred to as 'undoing local modifications'.

# Discard changes in the working directory for a specific file
git restore <file_path>

# Example:
git restore src/main.js

Using git restore to discard uncommitted changes

# If the file was also staged, you might need to unstage it first
git restore --staged <file_path>
git restore <file_path>

# Alternatively, using the older 'git checkout' command:
git checkout -- <file_path>

# Example:
git checkout -- src/main.js

Discarding staged and uncommitted changes using git restore or git checkout

Scenario 2: Reverting a Committed File to a Previous Commit

When a file has been committed, you might want to revert it to a version from an earlier commit. This doesn't erase history but rather creates a new commit that undoes the changes to that specific file from a past commit. There are a few ways to achieve this, depending on whether you want to revert to the state of a specific commit or just undo the changes introduced by a particular commit.

1. Find the desired commit hash

First, you need to identify the commit hash (or a reference like HEAD~1 for the previous commit) that contains the version of the file you want to restore. You can use git log to browse commit history.

2. Checkout the file from the specific commit

Use git restore --source (or the older git checkout) to bring the file's content from that commit into your working directory. This will not stage the file automatically.

3. Stage and commit the reverted file

After restoring the file to your working directory, you need to stage it (git add) and then commit it (git commit) to record this reversion as a new change in your repository's history.

# 1. Find the commit hash (e.g., using git log)
git log -- <file_path>

# Example output:
# commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0 (HEAD -> main)
# Author: Your Name <your.email@example.com>
# Date:   Mon Jan 1 12:00:00 2023 +0000
#
#     Update feature X
#
# commit f0e9d8c7b6a5f4e3d2c1b0a9f8e7d6c5b4a3f2e1
# Author: Another Name <another.email@example.com>
# Date:   Sun Dec 31 10:00:00 2022 +0000
#
#     Initial implementation of feature X

# Let's say you want to revert 'src/config.js' to the state of commit 'f0e9d8c7'

# 2. Restore the file from the specific commit to your working directory
git restore --source f0e9d8c7 -- src/config.js

# (Older method using checkout)
git checkout f0e9d8c7 -- src/config.js

# 3. Stage and commit the change
git add src/config.js
git commit -m "Revert src/config.js to version from f0e9d8c7"

Reverting a committed file to a specific past version

Scenario 3: Undoing the Introduction of a File

If you accidentally added and committed a file that should not be in the repository, you can remove it and then commit that removal. If you want to completely erase its history (which is more complex and generally discouraged for shared repositories), you'd need advanced tools like git filter-repo.

# Remove the file from your working directory and stage the removal
git rm <file_path>

# Commit the removal
git commit -m "Remove accidentally added file: <file_path>"

# Example:
git rm sensitive_data.txt
git commit -m "Remove sensitive_data.txt"

Removing an accidentally committed file