While dealing with different features in the development project, developers create multiple files. They add changes and update the file version from time to time. However, sometimes, it is required to view the old versions of the file to compare changes. For this corresponding purpose, Git allows users to view the old modifications of the file whenever they want.
This blog will describe the procedure to see an old version of a file with Git.
How to View an Old Version of a Particular File With Git?
To view or see an old version of a specific file in Git, check out the below-provided instructions:
- Navigate to the local repository.
- View repository content.
- Display commit history.
- Choose a desired commit id.
- View the old version of the file using the “git show <commit-id>:<file-name>” command.
Step 1: Switch to Local Repository
First, redirect to the particular local repository using the following command:
Step 2: List Repository Content
Then, view the current repository’s content:
According to the below output, the current repository contains three files. Choose the desired file whose version you want to view. For instance, we have selected the “new.txt” file to view its old version:
Step 3: View Commit History
Next, utilize the following command to display the commit history of the repository:
In the below screenshot, all the commits of the current repository can be seen. Copy the specific commit id of the selected file. For instance, we have copied the “43e5d18” commit id:
Step 5: View Old Version of File
To view the old version of the selected file, utilize the “git show” command along with the desired commit id and file name:
The below image displays all the versions of the “new.txt” file:
Moreover, to view the difference between the older version of the file, the “git show <-n> <file-name>” command can be used:
Here, “-2” is used to compare the last two versions of the “new.txt” file.
In the below-given output, you can observe the comparison between the last two versions of the desired file:
We have explained the easiest method to view an old version of a particular file in Git.
To view an older version of a file with Git, first switch to the local repository and view its content. Then, check the repository’s commit history and choose the particular commit id of the desired file. Next, run the “git show <commit-id>:<file-name>” command to view the old version of the file. This blog explained the method to view an old version of a file in Git.