Git

Restore File From Old Commit in Git

Developers create multiple files while dealing with an extensive development project. Those files are saved to the local Git repository by committing. Users even delete old files that are not in use anymore. However, they may need to utilize an old deleted file. In this situation, Git allows this to restore any file from old commits.

This blog will explain the procedure of restoring a file from an old commit in Git.

How to Restore File From Old Commit in Git?

To restore the file from an old commit in Git, try out the provided steps:

  • Navigate to the local repository.
  • List repository content.
  • View commit history.
  • Choose the desired commit id.
  • Restore the file by running the β€œgit restore –source=<commit-id> <file-name>” command.
  • Track and commit a file.

Step 1: Redirect to Local Repository

First, type out the below-stated command and switch to the particular local repository:

cd "C:\Git\Repo1"

Step 2: View Repository Content

Then, list the available content of the current repository:

ls

It can be observed that the working repository contains two files:

Step 3: Check Git Status

Next, view the current status of the working directory using the below-provided command:

git status

Step 4: View Commit History

Now, check the Git log to view the commit history:

git log --oneline

In the below output, the commits history can be seen. Choose the desired commit whose file needs to be restored. For instance, we have selected the β€œec44d52” commit id to restore the β€œT1.txt” file:

Step 5: Restore Particular File

To restore the specific file from the old commit, run the following command along with the desired commit id and file name:

git restore --source=ec44d52 T1.txt

Step 6: View Repository Status

Now, again check the repository’s current status:

git status

As you can see, the file β€œT1.txt” file has been restored, but it is untracked and uncommitted:

Step 7: Add File to Git Index

Then, add the untracked file to the Git staging area for tracking purposes:

git add T1.txt

Step 8: Commit File

Next, use the following command to commit the desired file:

git commit -m "T1.txt file added"

Step 9: Verify Changes

Ensure that desired file has been restored or not by viewing the repository’s content:

ls

It can be observed that now the repository is containing three files and β€œT1.txt” has been restored from the old commit successfully:

We have efficiently explained the method of restoring a file from old commits in Git.

Conclusion

To restore a particular file from an old commit that no longer exists in the project directory, first, navigate to the local repository. Then, view the repository’s commit history and select the desired commit id that needs to be restored. Next, run the β€œgit restore –source=<commit-id> <file-name>” command to restore the file. After that, stage and commit the file and verify changes. This blog explained the method to restore a specific file from an old commit in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.